1
votes

In my UserControl I have a 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}" -->

I tried to add data binding to it, & added some logic on checked & non-checked; so no need to add event to the same.

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

        }
    }

Tried alot wit binding, but no success. Finally added checked event & commented binding property. -

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

With this also, even when checkbox is unChecked, it doesn't fire event nor property with both checked & unchecked. In both - Binding & Event 1 state event is fired properly but the other isn't !!! Have also added TwoWay mode, tried with UpdateSourceTrigger in binding but no success.

Why is this Strange problem with Checkbox....

1

1 Answers

2
votes

For your event:

You also need to subscribe to the Unchecked event.

Change your xaml to:

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

Now both events will fire the same handler and if you set a breakpoint in your handler, you can see it get called.

For your binding

Not quite sure what you're trying with it. Firstly the property you define for your DP's are merely a convenience for you and the underlying framework does not invoke it's setter's or getter's when updating the DP value. Next up, not sure why you're calling a NotifyPropertyChanged("ShowLegends"); on a DP. If my assumtion is right and that actually corresponds to a INPC raise property changed implementation, it's not needed for DP's

Start with trying simple stuff. Something like:

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

and in your DataContext class have the corresponding property

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

You should see the Debug.WriteLine get invoked on changes to the IsChecked property. once you got to that stage add the rest of your logic step by step and validate if it still works and if it doesn't, it's prolly something you're adding causing it than a behavior of the system.

Update:

Download Link to sample

Attached sample should show three approaches. Event based, Simple Binding, Complex Binding connected to a DP from a custom control to then toggle that control's visibility.