【问题标题】:WPF, hide textbox when uncheckedWPF,未选中时隐藏文本框
【发布时间】:2016-03-28 01:20:11
【问题描述】:

当复选框值为 true 时,我尝试隐藏文本框,但当未选中时,文本框不会隐藏我可以做些什么来解决这个问题?

这是我的代码

private void textBox4_TextChanged(object sender, TextChangedEventArgs e)
    {
    }

    private void checkBox_Checked(object sender, RoutedEventArgs e)
    {
        Handle(sender as CheckBox);
    }
    private void checkBox_Unchecked(object sender, RoutedEventArgs e)
    {
        Handle(sender as CheckBox);
    }

    void Handle(CheckBox checkBox)
    {
        bool chkd = checkBox.IsChecked.Value;

        if (chkd)
        {
            textBox4.Visibility = Visibility.Visible;
        }
        else
        {
            textBox4.Visibility = Visibility.Hidden;
        }
    }

【问题讨论】:

  • 如何订阅选中/取消选中事件?
  • 您不...使用 XAML/WPF 您可以将 IsChecked 属性绑定到视图模型中的属性。

标签: c# wpf visual-studio


【解决方案1】:

你可以试试下面的解决方案->Binding to a WPF ToggleButton's IsChecked state

该解决方案基本上将复选框与它希望使用转换器而不是代码隐藏在 xaml 上的内容相关联。

【讨论】:

    【解决方案2】:

    只要使用这样的东西:

    private void checkBox_CheckChanged(object sender, RoutedEventArgs e)
    {
        textBox4.Visibility = (checkBox.IsChecked) ? Visibility.Visible : Visibility.Hidden;
    }
    

    像这样将它添加到 CheckChanged 事件中:

    checkBox.CheckedChanged += checkBox_CheckChanged;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      相关资源
      最近更新 更多