WPF中控件的类比

时间:2017-03-26 15:15:52

标签: c# wpf winforms

我有几个组合框,每组有3个无线电按钮(它们都在网格中)。在WinForms中,我可以使用Controls来按名称和索引访问控件,如:

groupbox1.Controls[0] 

groupbox1.Controls["radiobutton1"]

所以我需要在WPF中做的是:我有一个groupbox,我需要使用索引访问其中的某些radiobutton,就像我在WinForms中一样。在WPF中有可能吗?或者我怎样才能到达groupbox的孩子们的集合?

1 个答案:

答案 0 :(得分:0)

这个问题与任何一个评论问题都不完全重复。

我们假设您有以下设置

<GroupBox>
    <StackPanel x:Name="MyRadioButtons">
        <RadioButton x:Name="radio1">Radio1</RadioButton>
        <RadioButton x:Name="radio2">Radio2</RadioButton>
        <RadioButton x:Name="radio3">Radio3</RadioButton>
    </StackPanel>
</GroupBox>

您可以按索引或名称访问元素的子元素,然后转换为RadioButton:

(MyRadioButtons.Children[0] as RadioButton)             // By index
(MyRadioButtons.FindName("radio2") as RadioButton)      // By x:Name attr