0

After changing CheckBox.IsChecked by DataTrigger source value to which CheckBox.IsChecked has binding is not changing.

I have simple ViewModel

public class ViewModel : INotifyPropertyChanged
    {    
        private bool check1;    
        public bool Check1
        {
            get { return check1; }
            set { check1 = value; NotifyPropertyChanged(); }
        }    

        private bool check2;    
        public bool Check2
        {
            get { return check2; }
            set { check2 = value; NotifyPropertyChanged(); }
        }

        #region Notify
        ...    
        #endregion
    }

and simple XAML

<StackPanel Grid.Row="1">
        <CheckBox Content="Check1">
            <CheckBox.Style >
                <Style TargetType="{x:Type CheckBox}">
                    <Setter Property="IsChecked" Value="{Binding Check1, Mode=TwoWay}"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Check2}" Value="True">
                            <Setter Property="IsChecked" Value="True"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </CheckBox.Style>
        </CheckBox>

        <CheckBox Content="Check2" IsChecked="{Binding Check2}"/>

        <TextBlock Text="{Binding Check1}" Name="uiCheckValue1"/>
        <TextBlock Text="{Binding Check2}" Name="uiCheckValue2"/>
    </StackPanel>

enter image description here When I check CheckBox2 the CheckBox1 becomes checked but source is not updated. How to make it update source?

enter image description here

1
  • Try "{Binding Check1, Mode=TwoWay]" Commented Aug 3, 2016 at 6:11

3 Answers 3

2

Your code seems to be wrong. You should emit the PropertyChanged event properly to update the view. Check below code :

public class ViewModel : INotifyPropertyChanged
{    
    private bool check1;    
    public bool Check1
    {
        get { return check1; }
        set 
        { 
            check1 = value; 
            PropertyChanged(value, new PropertyChangedEventArgs("Check1")); 
        }
    }    

    private bool check2;    
    public bool Check2
    {
        get { return check2; }
        set 
        { 
            check2 = value;
            PropertyChanged(value, new PropertyChangedEventArgs("Check1")); 
        }
    }

    #region Notify
    ...    
    #endregion
}

Also change the binding to TwoWay

<<CheckBox Content="Check2" IsChecked="{Binding Check2, Mode=TwoWay}"/>

<DataTrigger Binding="{Binding Check2, Mode=TwoWay}" Value="True">
<Setter Property="IsChecked" Value="True" />
</DataTrigger>
Sign up to request clarification or add additional context in comments.

Comments

2

A little late, but if you Change

<CheckBox Content="Check2" IsChecked="{Binding Check2}"/>

To

<CheckBox Content="Check2" IsChecked="{Binding Check2, UpdateSourceTrigger=PropertyChanged}"/>

you can get rid of all the code behind. The default for checkboxes is something like LostFocus, which is entirely annoying.

Comments

1

It's easy to say why: You loose the binding if you set IsChecked on True manually.
Delete the DataTrigger and change your ViewModel like this:

public bool Check2{
    get { return check2; }
    set { 
          check2 = value; 
          if (value == true) Check1 = true;
          NotifyPropertyChanged(); 
        }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.