wpf 在WPF中动态生成不同内容的RadioButtons集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13024441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:53:49  来源:igfitidea点击:

Dynamically Generate Set of RadioButtons With Different Content in WPF

c#wpfdynamicradio-button

提问by StonedJesus

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.

我是一名 C++ 开发人员,最近转向 C#。我正在开发一个 WPF 应用程序,我需要在其中动态生成 4 个单选按钮。我尝试了很多 RnD,但看起来这种情况很少见。

XAML:

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 Contentas follows:

现在是这样的场景:我应该生成这个单选按钮 4 次,不同Content如下:

<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:

我在我的 C++ 应用程序中这样做了,如下所示:

#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 0x80and base 0xc0and 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.

如果你在上面看到,每一次的循环中运行的内容名称将变为Base 0x0Base 0x40base 0x80base 0xc0和第一组的单选按钮真正的切换状态。因此,如果您注意到所有这 4 个按钮都有单按钮单击方法,并且每个按钮都将根据索引执行操作。

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

我怎样才能在我的 WPF 应用程序中实现这一点?:)

回答by FanerYedermann

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

我打算为您编写一组代码,但意识到您的问题可能已经在这里得到解答: WPF/C# - 以编程方式创建和使用单选按钮的示例

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:

Xml:

<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#:

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.

只需添加内容、标签等所需的任何逻辑。