locked
I want to create a Button dynamicly,How to align it to Grid's border,for explem Grid(3,5)'s left vertical border? RRS feed

  • Question

  • I want to create a Button dynamicly,How to align it to Grid's border,for explem Grid(3,5)'s left vertical  border?
    Tuesday, March 11, 2014 10:35 AM

Answers

  • Iam sorry,The Grid has some rowes and columns,How to align a button to Grid(row,column)?

    mybUTTON.SetValue(Grid.RowProperty, 1);

    you can also assign grid.column, grid.rowspan and grid.columnspan the same way.

    • Marked as answer by yang2013yang Wednesday, March 12, 2014 3:30 AM
    Tuesday, March 11, 2014 4:48 PM
  • Use SetValue to set the row and column properties.

    For this in xaml:    <Button Grid.Row="3" Grid.Column="5" />

    The equivalent in C# is:

        Button btn = new Button();
        btn.SetValue(Grid.RowProperty, 3);
        btn.SetValue(Grid.ColumnProperty, 5);
    

    Tuesday, March 11, 2014 4:54 PM

All replies

  • Here's how you create the button dynamically....Just create the button within a loop so that it could be created any number of times you want...

    So just have this in a click event..

    private void Grid_View_Btn_1_Click(object sender, System.Windows.RoutedEventArgs e)
    {   
         Dispatcher.BeginInvoke(() => {         
         StackPanel panel = new StackPanel();
         panel.Orientation = System.Windows.Controls.Orientation.Vertical;
         int i;
         for (i=0;i<5;i++)
         {
                Button btn = new Button() { Content = "Button" };
                btn.Width=130;
                btn.Height = 66;
                // btn.Margin = new Thickness(0,0,0,0)//try this if you use grid
                //grid.Children.Add(btn);
                panel.Children.Add(btn);
         }
    
         grid.Children.Add(panel);
    
      }); } 


    Happy Coding...
    Tuesday, March 11, 2014 10:54 AM
  • Iam sorry,The Grid has some rowes and columns,How to align a button to Grid(row,column)?
    Tuesday, March 11, 2014 12:21 PM
  • You have to adjust the margin of either the grid or the button... Or else you can easily drag the corners till you align both of them......
    Tuesday, March 11, 2014 2:37 PM
  • Iam sorry,The Grid has some rowes and columns,How to align a button to Grid(row,column)?

    mybUTTON.SetValue(Grid.RowProperty, 1);

    you can also assign grid.column, grid.rowspan and grid.columnspan the same way.

    • Marked as answer by yang2013yang Wednesday, March 12, 2014 3:30 AM
    Tuesday, March 11, 2014 4:48 PM
  • Use SetValue to set the row and column properties.

    For this in xaml:    <Button Grid.Row="3" Grid.Column="5" />

    The equivalent in C# is:

        Button btn = new Button();
        btn.SetValue(Grid.RowProperty, 3);
        btn.SetValue(Grid.ColumnProperty, 5);
    

    Tuesday, March 11, 2014 4:54 PM