WPF 单选按钮检查

本文关键字:检查 单选按钮 WPF | 更新日期: 2023-09-27 18:36:18

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

WPF 单选按钮检查

给你的RadioButton x:NameName,比如

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

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

if(MyRadioButton.IsChecked == true)
{
}

你可以这样找到

使用 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? .