0

I have a CheckBox set up something like this:

<CheckBox Unchecked="checkBox_Unchecked">
    <CheckBox.Style>
        <Style TargetType="CheckBox">
            <Setter Property="IsChecked" Value="True"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding MyVal}" Value="{x:Null}">
                    <Setter Property="IsChecked" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </CheckBox.Style>
</CheckBox>

Is there any way to get the Unchecked event to fire when my Setter sets IsChecked = False?

2
  • 1
    When IsChecked of Checkbox is False, the UnChecked event should raise. That is the default behavior.
    – Jawahar
    Dec 12, 2014 at 2:26
  • The weird thing is, it seems to be in one case and not in another case. I guess I should specify that by "force" I mean just make it more verbose.
    – Hexum064
    Dec 12, 2014 at 2:46

2 Answers 2

0

This may not be a procedural approach; But for your statement, I guess I should specify that by "force"... You can make this using this work around;

1) In your ViewModel have a boolean property listening to 'INotifyPropertyChanged'

2) Let the default value of property be true;

3) In your constructor re-assign the property's value to 'false'.

4) This will trigger the unchecked event;

XAML:

<CheckBox IsChecked="{Binding CheckboxChecked}" Unchecked="ToggleButton_OnUnchecked"/>

C#:

    public partial class MainWindow : INotifyPropertyChanged
    {
        private bool m_CheckboxChecked = true;

        public bool CheckboxChecked
        {
            get { return m_CheckboxChecked; }
            set { m_CheckboxChecked = value; OnPropertyChanged("CheckboxChecked"); }
        }

        public MainWindow()
        {
            DataContext = this;
            InitializeComponent();
            CheckboxChecked = false;
        }

        private void ToggleButton_OnUnchecked(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("Un-Checked");
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
0

You need a Setter for the Case IsChecked = false too. Also I would recommend to bind to a Command in the ViewModel Try this:

            <Style.Triggers>
            <Trigger Property="IsChecked" Value="true">
                <Setter Property="Command" Value="{Binding ElementName=this, Path=DataContext.ItemCheckedCommand}"></Setter>
            </Trigger>
            <Trigger Property="IsChecked" Value="false">
                <Setter Property="Command" Value="{Binding ElementName=this, Path=DataContext.ItemCheckedCommand}"></Setter>
            </Trigger>
        </Style.Triggers>

And define a command where you have your logic in the VM:

public RelayCommand ItemCheckCommand {get; private set;}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.