wpfcheckboxdata-bindingdependency-properties

IsChecked binding for a checkbox inside a usercontrol is not fi


In the UserControl.xaml:

<UserControl x:Class="OurNameSpace.SystemCalibration.Controls.PortSelector"
             xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
             xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
             xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 
             xmlns:d=http://schemas.microsoft.com/expression/blend/2008 
             xmlns:local="clr-namespace:OurNamespace.SystemCalibration.Controls"
             xmlns:converters="clr-namespace:OurNamespace.SystemCalibration.Converters"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             x:Name="portSelector"
             mc:Ignorable="d">
<Grid …..
               <StackPannel ….
<CheckBox Content="Port 4" Margin="0 5 0 5"
                      IsChecked="{Binding cb4_IsChecked, ElementName=portSelector, UpdateSourceTrigger=PropertyChanged}">
                <CheckBox.Visibility>
                    <MultiBinding Converter="{StaticResource maxPortsToVisibilityConverter}">
                        <Binding Path="MaxPorts" ElementName="portSelector"/>
                        <Binding Source="{StaticResource port4}"/>
                    </MultiBinding>
                </CheckBox.Visibility>
              </CheckBox>
</StackPanel>
</Grid>

In the usercontrol.xaml.cs:

public partial class PortSelector : UserControl
{
public static readonly DependencyProperty cb4_isCheckedProperty = DependencyProperty.Register(nameof(cb4_IsChecked), typeof(bool), typeof(PortSelector));

public bool cb4_IsChecked
        {
            get => (bool)GetValue(cb4_isCheckedProperty);
            set
            {
                SetValue(cb4_isCheckedProperty, value);
                UpdatedSelectedPortsString();
            }
       }
}

If I put a break on the line with the call to "UpdatedSelectedPortsString" , the breakpoint never gets hit.

This usercontrol is hosted inside another usercontrol which is inside a TabControl. When I click on the checkbox, I expected to execute the Set part of my dependencyproperty, but that code does not seem to get called at all. I have even tried adding a fake value converter to the IsChecked binding and code inside the converter does execute, but not the setter. If instead of binding IsChecked, I add a Command binding with the dependency property and put the command handler in the same usercontrol.xaml.cs code-behind partial class, it works as well. The only thing that seems to not work is binding the IsChecked directly.


Solution

  • You must use PropertyChangedCallback delegate like below:

    public static readonly DependencyProperty cb4_IsCheckedProperty = DependencyProperty.Register(nameof(cb4_IsChecked), typeof(bool), typeof(PortSelector), new PropertyMetadata(false, cb4_IsCheckedChangedCallback));
        public bool cb4_IsChecked
        {
            get { return (bool)GetValue(cb4_IsCheckedProperty); }
            set { SetValue(cb4_IsCheckedProperty, value); }
        }
        private static void cb4_IsCheckedChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            PortSelector uc = d as PortSelector;
            uc.UpdatedSelectedPortsString();
        }
    

    The "cb4_IsCheckedChangedCallback" method is called every time IsChacked is changed.

    If this didn't work. This code should be used for binding:

    IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PortSelector}}, Path=cb4_IsChecked}"