【问题标题】:WPF radio button checkingWPF单选按钮检查
【发布时间】:2013-10-28 23:27:00
【问题描述】:

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

【问题讨论】:

    标签: c# wpf radio-button


    【解决方案1】:

    x:NameName 给你的RadioButton 喜欢

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

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

    if(MyRadioButton.IsChecked == true)
    {
    }
    

    【讨论】:

      【解决方案2】:

      你可以这样发现

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

      【讨论】:

      • 如果你写的是 if(RBMetLines.HasValue && RBMetLines.Value) ,你就不必解释你的代码了
      猜你喜欢
      • 1970-01-01
      • 2019-05-29
      • 2019-01-15
      • 2016-08-17
      • 2012-12-07
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多