【问题标题】:WPF Checkbox Event & Binding not workingWPF复选框事件和绑定不起作用
【发布时间】:2014-03-16 23:06:18
【问题描述】:

在我的用户控件中,我有一个复选框

            <CheckBox DockPanel.Dock="Left" VerticalAlignment="Bottom"  VerticalContentAlignment="Bottom"  x:Name="showLegendsChk" Margin="10,0,0,0"
                  Content="View Legends" Checked="showLegendsChk_Checked" />
        <!--IsChecked="{Binding ElementName=CrossSecViewWnd, Path=ShowLegends, Mode=TwoWay}" -->

我尝试为其添加数据绑定,并在选中和未选中添加了一些逻辑;所以不需要添加事件。

    private bool showLegendsWnd;
    public CrossSectionalViewControl() {
        FillLegends();
        ShowLegends = false;
    }

        // Using a DependencyProperty as the backing store for 
     //IsCheckBoxChecked.  This enables animation, styling, binding, etc...
   public static readonly DependencyProperty ShowLegendsProperty =
       DependencyProperty.Register("ShowLegends", typeof(bool),
       typeof(CrossSectionalViewControl), new UIPropertyMetadata(false));

    public bool ShowLegends
    {
        get { return showLegendsWnd; }
        set
        {
            showLegendsWnd = value;
            NotifyPropertyChanged("ShowLegends");
            if (showLegendsWnd == true)
                legendWrap.Visibility = System.Windows.Visibility.Visible;
            else
                legendWrap.Visibility = System.Windows.Visibility.Hidden;

            Console.WriteLine("Show Legends = " + showLegendsWnd + " Chk Value = " + showLegendsChk.IsChecked);

        }
    }

尝试了很多机智绑定,但没有成功。最后添加了检查事件和注释绑定属性。 -

    private void showLegendsChk_Checked(object sender, RoutedEventArgs e)
    {
            showLegendsWnd = (bool)showLegendsChk.IsChecked;
            Console.WriteLine("CHK Show Legends = " + showLegendsWnd + " Chk Value = " + showLegendsChk.IsChecked);

            if (showLegendsWnd == true)
                legendWrap.Visibility = System.Windows.Visibility.Visible;
            else
                legendWrap.Visibility = System.Windows.Visibility.Hidden;

            legendWrap.UpdateLayout();
    }

同样,即使未选中复选框,它也不会触发事件或属性,同时选中和未选中。 在这两种情况下 - 绑定和事件 1 状态事件被正确触发,但另一个不是!还添加了 TwoWay 模式,尝试在绑定中使用 UpdateSourceTrigger 但没有成功。

为什么 Checkbox 会出现这个奇怪的问题....

【问题讨论】:

    标签: c# wpf events data-binding checkbox


    【解决方案1】:

    您的活动:

    您还需要订阅Unchecked 事件。

    将您的 xaml 更改为:

    <CheckBox x:Name="showLegendsChk"
              DockPanel.Dock="Left"
              VerticalAlignment="Bottom"
              VerticalContentAlignment="Bottom"              
              Margin="10,0,0,0"
              Content="View Legends"
              Checked="showLegendsChk_Checked"
              Unchecked="showLegendsChk_Checked" />
    

    现在这两个事件将触发同一个处理程序,如果您在处理程序中设置断点,您可以看到它被调用。

    供您装订

    不太确定你在用它做什么。首先,您为 DP 定义的属性只是为您提供便利,并且底层框架在更新 DP 值时不会调用它的 setter 或 getter。接下来,不确定您为什么要在 DP 上调用NotifyPropertyChanged("ShowLegends");。如果我的假设是正确的并且实际上对应于 INPC raise 属性更改实现,那么 DP 不需要它

    从尝试简单的东西开始。比如:

    <CheckBox x:Name="chkBox"
              IsChecked="{Binding IsChecked}" />
    

    并且在您的DataContext 类中具有相应的属性

    private bool _isChecked;
    public bool IsChecked {
      get { return _isChecked; }
      set {
        _isChecked = value;
        Debug.WriteLine(string.Format("Checkbox check state changed to: {0}", _isChecked ? "Checked" : "Not Checked"));
      }
    }
    

    您应该会看到 Debug.WriteLine 在更改 IsChecked 属性时被调用。一旦你到了那个阶段,逐步添加其余的逻辑,并验证它是否仍然有效,如果没有,这很可能是你添加的东西而不是系统的行为。

    更新:

    Download Link to sample

    附加示例应显示三种方法。基于事件、简单绑定、复杂绑​​定从自定义控件连接到 DP,然后切换该控件的可见性。

    【讨论】:

    • Viv,感谢 UnChecked 活动。 REg Binding,我只能看到 Debug 行一次,在那之后我改变了多少状态,Debug 行没有出现 - 所以它没有被触发/调用。
    • @Tvd 绑定对我来说很好用。我已经编辑了答案并附上了一个示例,您可以下载并尝试。就像我在答案中提到的那样。随附的示例显示了三种方法。基于事件的简单绑定以及复杂绑定,其中 ischecked 绑定到自定义控件的 DP,然后切换该控件的可见性。所有三种方法都可以正常工作。已经测试过了。
    猜你喜欢
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    相关资源
    最近更新 更多