Coffeehouse Thread

5 posts

Simple WPF question

Back to Forum: Coffeehouse
  • User profile image

    This is probably very simple, but Google has not helped me this time.

    I have a grid defined in XAML, I want to add radiobuttons programatically. The problem I have right now is that it's adding the radiobuttons on top of each other, but google searches shows others doing the same thing.

    RadioButton r1 = new RadioButton();
    RadioButton r2 = new RadioButton();

    Can anyone help? Thanks!


    (hmm, no preview button in here..)

  • User profile image

    @pavone:You need to set the Grid.Row and Grid.Column attached properties for each radio button like so:


    Grid.SetRow(r1, 0);
    Grid.SetColumn(r1, 0);
    Grid.SetRow(r2, 1);
    Grid.SetColumn(r2, 0);

  • User profile image

     Thanks DCMonkey, there was something else to add to get it to work.

    RowDefinition row = new RowDefinition(); // do this for number of rows
    row.Height = new GridLength(20); // to get it to right size


    Seems a bit convoluted, but it works. 

  • User profile image

    I'm not sure what the use case is, but maybe something with stackpanel's?

  • User profile image

    Yes, I'd do this in a StackPanel instead of a Grid, depending on your layout requirements.

    You may also want to think about doing this all in XAML with a ListView and a DataTemplate.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.