WPF单选按钮检查

时间:2013-10-20 13:49:05

标签: c# wpf radio-button

现在即时编码我的第一个gui程序,我有一个问题(我知道它非常简单,但我找不到答案)。我有2个单选按钮,彼此分开,我无法检查是否单选按钮是选中,这是我的代码:

 <RadioButton Content="Metinės"
                 Checked="RadioButton_Checked_1"
                 HorizontalAlignment="Left"
                 Margin="393,124,0,0"
                 Height="21"
                 Width="101"
                 FontSize="14"
                 ClickMode="Press"
                 VerticalAlignment="Top"
                 FontFamily="Segoe WP Semibold"/>

和c#

  if (RadioButton_Checked == true)
            {
                //program code
            }

2 个答案:

答案 0 :(得分:11)

x:NameName提供给您的RadioButton

<RadioButton x:Name="MyRadioButton" Content="Metinės"/>

然后在代码后面你可以检查

if(MyRadioButton.IsChecked == true)
{
}

答案 1 :(得分:4)

你可以找到这样的

使用x:Name ="RBMetLines"提供您的单选按钮名称,并在

后面的代码中访问该名称
<RadioButton Content="Metinės"
             x:Name="RBMetLines"
             Checked="RBMetLines_Checked"
             HorizontalAlignment="Left"
             Margin="393,124,0,0"
             Height="21"
             Width="101"
             FontSize="14"
             ClickMode="Press"
             VerticalAlignment="Top"
             FontFamily="Segoe WP Semibold"/>

和C#代码背后的

private void RBMetLines_Checked(object sender, RoutedEventArgs e)
{
    if(Convert.ToBoolean(RBMetLines.IsChecked))
    {
        //program code
    }
}

我已将IsChecked转换为布尔值,因为在WPF中IsChecked为bool?