Dynamically Generate Set of RadioButtons With Different Content in WPF

10,593

I was going to write a set of code for you, but realized your question is probably already answered here: WPF/C# - example for programmatically create & use Radio Buttons

It's probably the cleanest way of doing it, depending on your requirements of course. If you want the simplest case, here it is:

Xaml:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid >
        <StackPanel x:Name="MyStackPanel" />

    </Grid>
</Window>

C#:

    public MainWindow()
    {
        InitializeComponent();

        for (int i = 0; i < 4; i++)
        {
            RadioButton rb = new RadioButton() { Content = "Radio button " + i, IsChecked = i == 0  };
            rb.Checked += (sender, args) => 
            {
                Console.WriteLine("Pressed " + ( sender as RadioButton ).Tag );
            };
            rb.Unchecked += (sender, args) => { /* Do stuff */ };
            rb.Tag = i;

            MyStackPanel.Children.Add( rb );
        }
    }

Just add in whatever logic you need for the content, tags and so on.

Share:
10,593

Related videos on Youtube

StonedJesus
Author by

StonedJesus

I joined Software Industry 14 Months back. I have 1 Year Experience In Qt and Currently Learning WPF. I am the owner of India's Premier Metal Portal METAL WANI. I am a journalist and Drummer of Thrash Metal band FORSAKEN.

Updated on May 25, 2022

Comments

  • StonedJesus
    StonedJesus about 2 years

    I am a C++ developer and recently shifted to C#. I am working on a WPF app where I need to dynamically generate 4 radio buttons. I tried to do lot of RnD but looks like this scenario is rare.

    XAML:

    <RadioButton Content="Base 0x" Height="16" Name="radioButton1" Width="80" />
    

    Now here is the scenario: I should generate this radio button 4 times with different Content as follows:

    <RadioButton Content = Base 0x0 />
    <RadioButton Content = Base 0x40 />
    <RadioButton Content = Base 0x80 />
    <RadioButton Content = Base 0xc0 />
    

    I had done this in my C++ application as follows:

    #define MAX_FPGA_REGISTERS 0x40;
    
    for(i = 0; i < 4; i++)
    {
        m_registerBase[i] = new ToggleButton(String(T("Base 0x")) + String::toHexString(i * MAX_FPGA_REGISTERS));       
        addAndMakeVisible(m_registerBase[i]);
        m_registerBase[i]->addButtonListener(this);
    }
    m_registerBase[0]->setToggleState(true); 
    

    If you notice above, Every-time for loop runs Content name becomes Base 0x0, Base 0x40, base 0x80 and base 0xc0 and sets the toggle state of first radiobutton as true. Thus if you notice there will be single button click method for all these 4 buttons and based on index each will perform operation.

    How can i achieve this in my WPF app? :)

  • StonedJesus
    StonedJesus over 11 years
    Thanks a lot buddy :) MVVM is the aproach i would like to take. But I can see he has a 2 button click events. Is that required in my MVVM approach?
  • FanerYedermann
    FanerYedermann over 11 years
    No it isn't required. I would also like to point out that the event handlers are added in a "non-MVVM" way, as they are set in the window's code-behind rather than through XAML and the view model. What you would instead do is set a binding on the IsChecked property of the radiobutton, using a style. In any case, you can subscribe to or ignore whatever events you want.
  • StonedJesus
    StonedJesus over 11 years
    Well this looks good. I tried to implement the MVVM approach and that looks quiet good. YOur approach is much simpler too :)
  • StonedJesus
    StonedJesus over 11 years
    Ideally I would like to have a index with it which gives me whichever radiobutton i have selected. Since the buttonclick method is same for all, ID would help me execute the logic for the particular radiobutton slected. How can I do that in MVVM way? :)
  • FanerYedermann
    FanerYedermann over 11 years
    Have a look at this: stackoverflow.com/questions/2284752/… The first answer there should help you if you want to do it purely with MVVM. Otherwise, try my edit of the initialization code above.
  • StonedJesus
    StonedJesus over 11 years
    Thanks buddy :) YOu been helpful :)