C# WPF 单选按钮检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19478120/
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-08-10 15:05:41  来源:igfitidea点击:

WPF radio button checking

c#wpfradio-button

提问by pauliustack

Now im coding my first gui program and i have a problem(I know its very simple,but i cant find an answer for it).I have 2 radio buttons,separeted from eachother,and i cant check if radio button is checked ,here`s my code:

现在我正在编写我的第一个 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"/>

And c#

和 C#

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

采纳答案by Nitin

Give x:Nameor Nameto your RadioButtonlike

x:NameName给你RadioButton喜欢的

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

and then in code behind you can check

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

if(MyRadioButton.IsChecked == true)
{
}

回答by Nikhil Agrawal

You can find out like this

你可以这样发现

Give your Radio Button name using x:Name ="RBMetLines"and access that in code behind

使用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"/>

and in C# code behind

并在后面的 C# 代码中

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

I have converted IsChecked to Boolean because in WPF IsChecked is bool?.

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