WPF复选框活动&绑定不起作用

时间:2014-02-17 15:16:02

标签: c# wpf events data-binding checkbox

在我的UserControl中,我有一个Checkbox

            <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}" -->

我尝试添加数据绑定到它,&amp;在check&amp;添加了一些逻辑非检查;所以不需要添加事件。

    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);

        }
    }

尝试了很多机智绑定,但没有成功。最后添加了检查事件&amp;评论绑定属性。 -

    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();
    }

这样,即使复选框是unChecked,它也不会同时检查&amp;选中。 在两者中 - 绑定&amp;事件1状态事件正常启动但另一个不是!还添加了TwoWay模式,尝试使用UpdateSourceTrigger绑定但没有成功。

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

1 个答案:

答案 0 :(得分:2)

对于您的活动:

您还需要订阅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提升属性改变了实现,那么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,然后切换控件的可见性。