【问题标题】:How to get the value of the checked radiobutton in wpf如何在wpf中获取选中的单选按钮的值
【发布时间】:2013-04-08 19:46:40
【问题描述】:

我在网格面板中有四个 RadioButtons,但是当我这样做时:

<GroupBox x:Name="radioButtons">
    <RadioButton Content="1" Height="16" HorizontalAlignment="Left" Margin="10,45,0,0" Name="status1" VerticalAlignment="Top" />
    <RadioButton Content="2" Height="16" HorizontalAlignment="Left" Margin="10,67,0,0" Name="status2" VerticalAlignment="Top" />
    <RadioButton Content="3" Height="16" HorizontalAlignment="Left" Margin="10,89,0,0" Name="status3" VerticalAlignment="Top" />
    <RadioButton Content="4" Height="16" HorizontalAlignment="Left" Margin="10,111,0,0" Name="status4" VerticalAlignment="Top" />
</GroupBox>

上面写着:

错误 1 ​​对象“GroupBox”已经有一个子对象,不能添加“RadioButton”。 'GroupBox' 只能接受一个孩子。

最后三个RadioButtons 说:

“内容”属性设置了多次。

我的GroupBox 怎么了?此外,在我的代码中,我想访问被检查的RadioButton(最好是int)。我该怎么做呢?我尝试在 Google 中查找,发现了很多结果,但我一个都看不懂。

【问题讨论】:

    标签: c# wpf radio-button gridpanel


    【解决方案1】:

    GroupBox 只能容纳 1 个项目,因此尝试多次设置 GroupBox 的 Content 属性时出错。

    因此,将其设为 Layout 项,然后将 RadioButtons 放入其中。现在,你设置一次Content,也就是StackPanel,那个布局项可以容纳很多孩子->RadioButtons。

    <GroupBox x:Name="radioButtons">
      <StackPanel>
        <RadioButton Name="status1"
                      Height="16"
                      Margin="10,45,0,0"
                      HorizontalAlignment="Left"
                      VerticalAlignment="Top"
                      Content="1" />
        <RadioButton Name="status2"
                      Height="16"
                      Margin="10,67,0,0"
                      HorizontalAlignment="Left"
                      VerticalAlignment="Top"
                      Content="2" />
        <RadioButton Name="status3"
                      Height="16"
                      Margin="10,89,0,0"
                      HorizontalAlignment="Left"
                      VerticalAlignment="Top"
                      Content="3" />
        <RadioButton Name="status4"
                      Height="16"
                      Margin="10,111,0,0"
                      HorizontalAlignment="Left"
                      VerticalAlignment="Top"
                      Content="4" />
      </StackPanel>
    </GroupBox>
    

    至于你的第二个问题,Christian Mosers WPF Tutorial.net 有一个不错的样本。不明白的可以先看看Binding和Converter这两个话题。

    以非 MVVM 方式检查RadioButton 的一种非常粗略的通知方式:

    private void RadioButtonChecked(object sender, RoutedEventArgs e) {
      var radioButton = sender as RadioButton;
      if (radioButton == null)
        return;
      int intIndex = Convert.ToInt32(radioButton.Content.ToString());
      MessageBox.Show(intIndex.ToString(CultureInfo.InvariantCulture));
    }
    

    然后,在 xaml 中的每个 RadioButtons 中,添加 Checked="RadioButtonChecked"。

    【讨论】:

    • 感谢它对我的帮助!你能指导我如何让谁被签入代码吗?
    • 我在答案的最后部分添加了一个链接。 wpftutorial.net/RadioButton.html 是实现您所需要的一个相当不错的示例。
    • 添加了一个绑定到 RadioButton 的 Checked 事件的函数。 intIndex 应该是一个 int 值,前提是 RadioButton 的内容已相应设置
    • 首先感谢您的帮助!其次,我不知道如何将它绑定到我的主代码,因为我使用以下代码将我的 xaml 连接到我的主代码: 和我的 MainWindowViewModel 已经继承了 DependencyObject,所以我不能继承 IValueConverter..
    • 你不要让你的 ViewModel 实现 IValueConverter。为它创建一个单独的类。与示例在名为“EnumMatchToBooleanConverter”的链接中显示的完全一样。
    【解决方案2】:

    如果你想要很多元素或控件,那么你必须将它们放在布局容器中。

    • 网格
    • 堆栈面板
    • 停靠面板
    • WrapPanel
    • 等等……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 2011-05-24
      • 2012-07-18
      相关资源
      最近更新 更多