CheckBox's checked state doesn't change

Emon Haque 3,176 Reputation points
2020-10-17T00:39:07.25+00:00

I've a Custom Control, Filter:

public class Filter : CheckBox
{
    static Filter() { DefaultStyleKeyProperty.OverrideMetadata(typeof(Filter), new FrameworkPropertyMetadata(typeof(Filter)));}
}

in the Generic.xaml, I've set the IsChecked property to true:

<Style BasedOn="{StaticResource {x:Type CheckBox}}" TargetType="{x:Type local:Filter}">
    <Setter Property="IsThreeState" Value="False"/>
    <Setter Property="IsChecked" Value="True"/>
    <Setter Property="Template">
        ...
    </Setter>
</Style>

in my view the IsChecked is bound to a property State:

<cc:Filter Content="Test" IsChecked="{Binding State, Mode=OneWayToSource}"/>

and in ViewModel, I haven't initialized the State:

bool state;
public bool State
{
    get { return state; }
    set { state = value; Tenants.Refresh(); }
}

Now, when I launch the app, State remains false.

Developer technologies | Windows Presentation Foundation
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2020-10-18T09:13:30.533+00:00

    It seems that bound properties have priority over styles. If you want to set the initial value of IsChecked to True, then try this:

    IsChecked="{Binding State, Mode=OneWayToSource, FallbackValue=True }"

    Maybe it is also possible to bind FallbackValue to the value of the style.

    1 person found this answer helpful.

  2. Emon Haque 3,176 Reputation points
    2020-10-18T17:21:36.9+00:00

    One workaround is override OnApplyTemplate and set IsChecked=true there and if I do so and keep Mode=OneWayToSource in binding, it hits set twice, first with false and then with true. If I remove the Mode in binding, it hits set once with true.

    In some other custom controls I've set default style with Setter Property ... Value ..., for example:

    <Setter Property="TextSearch.TextPath" Value="Name"/>
    <Setter Property="IsSynchronizedWithCurrentItem" Value="True"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="Focusable" Value="False"/>
    <Setter Property="BorderThickness" Value="0 0 1 0"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    ....
    

    and all these works normally, I don't have to override OnApplyTemplate BUT in case of CheckBox, I've to, who knows why!

    0 comments No comments

  3. DaisyTian-1203 11,646 Reputation points
    2020-10-28T05:34:07.427+00:00

    I can't reproduce your error , could you reproduce this issue based on my sample code?And could you tell me what framwork did your project target?
    MainWindow.xaml.cs:

      public partial class MainWindow : Window, INotifyPropertyChanged  
        {  
            public MainWindow()  
            {  
                InitializeComponent();  
                //State = true;  
                var test = tstFilter.IsChecked;  
            }  
            bool state;  
      
            protected void OnPropertyChanged(string propertyName)  
            {  
                if (PropertyChanged != null)  
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
            }  
            public event PropertyChangedEventHandler PropertyChanged;  
      
            public bool State  
            {  
                get { return state; }  
                set { state = value;   
                    //this.OnPropertyChanged("State");   
                }  
            }  
            public override void OnApplyTemplate()  
            {  
                tstFilter.IsChecked = true;  
                base.OnApplyTemplate();  
            }  
        }  
    

    MainWindow.Xaml

    <Window x:Class="teststate.MainWindow"  
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
            xmlns:local="clr-namespace:teststate"  
            mc:Ignorable="d"  
            Title="MainWindow" Height="450" Width="800">  
        <Grid>  
            <local:filter Content="Test" x:Name="tstFilter" IsChecked="{Binding State, Mode=OneWayToSource}" >  
            </local:filter>  
            <TextBlock Text="{Binding IsChecked, ElementName=tstFilter}" Height="50" />  
            <TextBlock Text="{Binding State}" Foreground="Red" Height="50" Margin="0,226,0,146.5" />  
        </Grid>  
    </Window>  
      
    

    Custom Control filter.cs:

     public class filter : CheckBox  
        {  
            static filter()  
            {  
                DefaultStyleKeyProperty.OverrideMetadata(typeof(filter), new FrameworkPropertyMetadata(typeof(filter)));  
            }  
        }  
    

    Generic.xaml:

     <Style TargetType="{x:Type local:filter}">  
            <Setter Property="Template">  
                <Setter.Value>  
                    <ControlTemplate TargetType="{x:Type local:filter}">  
                        <Grid>  
                        <Border Background="Black"  
                                BorderBrush="{TemplateBinding BorderBrush}"  
                                BorderThickness="{TemplateBinding BorderThickness}"  
                                Width="100" Height="100">  
                        </Border>  
                        <TextBlock Text="test"></TextBlock>  
                        </Grid>  
                    </ControlTemplate>  
                </Setter.Value>  
            </Setter>  
            <Setter Property="IsThreeState" Value="False"/>  
            <Setter Property="IsChecked" Value="True"/>  
        </Style>  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.