locked
image and textblock inside button with Aligment

    Question

  • Dear team,

                      i create a dynamic Buttons (around 50). in the button i used stack panel for setting background image and Text.But the image is not in center and i am not able to access the Textblock

     void createquestionButton(int questioncount)
            {
                //Button btn;
                RowDefinition rowdef;
                GridQuestionButton.ColumnDefinitions.Add(new ColumnDefinition());
                GridQuestionButton.ColumnDefinitions.Add(new ColumnDefinition());
                GridQuestionButton.ColumnDefinitions.Add(new ColumnDefinition());
                int inc = 0;
    
                int rowcount = questioncount / 3;
                for (int j = 0; j <= rowcount; j++)
                {
                    rowdef = new RowDefinition();
                    rowdef.Height = new GridLength(30); 
                    GridQuestionButton.RowDefinitions.Add(rowdef);
                    for(int K=0;K<3;K++)
                    {
                        if (inc >= questioncount) return;
                        btn = new Button();
                        btn.Name = slno[inc].ToString();
    
    
                        StackPanel sp = new StackPanel();
                        sp.Name = "SP-" + slno[inc].ToString();
                        TextBlock TB = new TextBlock();
                        TB.Text = slno[inc].ToString();
                        sp.Children.Add(TB);
                        Image IMG = new Image();
                        IMG.VerticalAlignment = VerticalAlignment.Center;
                        IMG.Height = 26;
                        IMG.Width = 31;
                        IMG.Source = new BitmapImage(new Uri("ms-appx:///Assets/NotAnswered.png"));
                        sp.Children.Add(IMG);
                        btn.Content = sp;
                        //btn.Content = slno[inc].ToString();
                        btn.FontSize = 11;
                        btn.SetValue(Grid.ColumnProperty, K);
                        btn.SetValue(Grid.RowProperty, j);
                        btn.Click += new RoutedEventHandler(NextQuestionclick);
                        GridQuestionButton.Children.Add(btn);
                        inc += 1;
                    }
                }
            }
    kindly help.
     
    Sunday, October 19, 2014 7:22 AM

Answers

  • Hi, Use this code for create Button 

    void createquestionButton(int questioncount)
            {
                int rowcount = questioncount / 3;
                RowDefinition rowdef;
                GridQuestionButton.ColumnDefinitions.Add(new ColumnDefinition());
                GridQuestionButton.ColumnDefinitions.Add(new ColumnDefinition());
                GridQuestionButton.ColumnDefinitions.Add(new ColumnDefinition());
                for (int j = 0; j <= rowcount; j++)
                {
                    for (int K = 0; K < 3; K++)
                    {
                        rowdef = new RowDefinition();
                        rowdef.Height = new GridLength(100);
                        GridQuestionButton.RowDefinitions.Add(rowdef);
                        Button btn = new Button();
                        btn.Content = j.ToString();
                        btn.FontSize = 25;
                        GridQuestionButton.Children.Add(btn);
                        Image image = new Image();
                        ImageBrush myBrush = new ImageBrush();
                        image.Source = new BitmapImage(new Uri("/Assets/mainbg.jpg",UriKind.RelativeOrAbsolute));
                        image.Stretch = Stretch.Fill;
                        myBrush.ImageSource = image.Source;
                        btn.Background = myBrush;
                        btn.SetValue(Grid.ColumnProperty, K);
                        btn.SetValue(Grid.RowProperty, j);
                    }
                }
            }

    O/P : 

    OR

    Just increase your grid size from 30 to 100;


    shah


    • Edited by MauliKShah Monday, October 20, 2014 10:30 AM
    • Marked as answer by mann madhan1 Monday, October 20, 2014 1:55 PM
    Monday, October 20, 2014 10:26 AM

All replies

  • Hi Mann,

    I build a simple test, you can see the following code snippet.

    Button btn = new Button();
                //add a grid control
                Grid grid = new Grid();
                //set an image as background to grid control
                BitmapImage bitTasImage = new BitmapImage();
                bitTasImage.UriSource = new Uri( new Uri("ms-appx:///"), "Assets/1.png");
                ImageBrush image = new ImageBrush();
                image.ImageSource = bitTasImage;
                image.Stretch = Stretch.Fill;
                grid.Background = image;
                //add a textblock control
                TextBlock tb = new TextBlock();
                tb.Text = DateTime.Now.ToString();
                grid.Children.Add(tb);
                //set button content
                btn.Content = grid;
                //add custom button to container
                sp.Children.Add(btn);
    

    If you still have questions, please feel free to let me know.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Monday, October 20, 2014 9:08 AM
    Moderator
  • Hi, Use this code for create Button 

    void createquestionButton(int questioncount)
            {
                int rowcount = questioncount / 3;
                RowDefinition rowdef;
                GridQuestionButton.ColumnDefinitions.Add(new ColumnDefinition());
                GridQuestionButton.ColumnDefinitions.Add(new ColumnDefinition());
                GridQuestionButton.ColumnDefinitions.Add(new ColumnDefinition());
                for (int j = 0; j <= rowcount; j++)
                {
                    for (int K = 0; K < 3; K++)
                    {
                        rowdef = new RowDefinition();
                        rowdef.Height = new GridLength(100);
                        GridQuestionButton.RowDefinitions.Add(rowdef);
                        Button btn = new Button();
                        btn.Content = j.ToString();
                        btn.FontSize = 25;
                        GridQuestionButton.Children.Add(btn);
                        Image image = new Image();
                        ImageBrush myBrush = new ImageBrush();
                        image.Source = new BitmapImage(new Uri("/Assets/mainbg.jpg",UriKind.RelativeOrAbsolute));
                        image.Stretch = Stretch.Fill;
                        myBrush.ImageSource = image.Source;
                        btn.Background = myBrush;
                        btn.SetValue(Grid.ColumnProperty, K);
                        btn.SetValue(Grid.RowProperty, j);
                    }
                }
            }

    O/P : 

    OR

    Just increase your grid size from 30 to 100;


    shah


    • Edited by MauliKShah Monday, October 20, 2014 10:30 AM
    • Marked as answer by mann madhan1 Monday, October 20, 2014 1:55 PM
    Monday, October 20, 2014 10:26 AM