WPF MessageBox Cancel checkbox check

1k views Asked by At

i've just came across a issue that i was unable to solve myself. So hopefully anyone could give me some ideas.

My problem:

So i have a checkbox which has a binding to a class variable:

//WPF
<CheckBox IsChecked="{Binding IsChecked}" Name="cb" />

//class
public class Mod
{
    bool _IsChecked;
    public bool IsChecked { get { return _IsChecked; } set { MainWindow.w.checkChanged(this, value); _IsChecked = value; } }
}

Now what i'm aiming for is when i click any of the checkboxes it will execute a method. This method will check different things and eventually if the checks are not met a MessageBox will show up with the buttons OK and Cancel.

When the user clicks Cancel i want to cancel the checkbox check by using my Binding on the Object.

mod.IsChecked = value;

However because the UI is not finished before the MessageBox shows up, it will finish the UI check controller after the MessageBox has been closed (I assume after it checks the Binding).

Check boxes have been clicked

The Toggle checkbox has just been clicked (first of the 3), but the UI thread is on pause because it waits on a InvokeEnd of the MessageBox.


I hope i've made my problem clear enough. I'm not a expert in threading, especially not with WPF/Form. So hopefully somebody can help me in the right direction.

Much appreciated, Nkmol

1

There are 1 answers

2
Panh On BEST ANSWER

If I understand this right, my initial thought would be to call the method from the setter of the checkbox's Property (in the ViewModel). From there, you should have the old value as well as the new value of the checkboxes and total control over what you want to do (pop up a messagebox in this case). There are a number of ways to do this (here's one way).

Side-note; the way you're "data-binding" seems "different" to me. Why not implement INotifyPropertyChanged and NotifyPropertyChanged in the setter of the checkboxes? The way you're doing it now seems to violate MVVM. Hope this helped and good luck!