【问题标题】:The analog of Controls in WPFWPF中的控件类比
【发布时间】:2017-03-26 15:15:52
【问题描述】:

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

groupbox1.Controls[0] 

或

groupbox1.Controls["radiobutton1"]

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

【问题讨论】:

标签: c# wpf winforms


【解决方案1】:

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

假设您有以下设置

<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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-04
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多