Click here to Skip to main content
15,438,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some logic running within a a Property. However, when attempting to use MessageBoxResult to find user's response (Yes or No) and based on that I would run the logic that will set the Property or quit/return, the set accessor is always called a second time.

In the example below, the issue happens only when the user clicks No:

private SomeType _SelectedItem;
public SomeType  SelectedItem
    get { return _SelectedItem; }
        if (_SelectedItem != value)
            var result = MessageBox.Show("Confirm your selection", "Confirm", MessageBoxButton.YesNo);
            if (result == MessageBoxResult.Yes)
                _SelectedItem = value;
Updated 17-Nov-14 10:19am
Maciej Los 17-Nov-14 13:48pm     CRLF
Why do you use var result instead of MessageBoxResult result =? if (result == MessageBoxResult.Yes) { _SelectedConfig = value; RaisePropertyChanged(); }
BeeDev 17-Nov-14 15:34pm    
To keep it short that is all.
Maciej Los 17-Nov-14 15:59pm    
So... Quickly, quickly... accept my answer ;)

1 solution

Please, see my comment to the question.

There is no strange MessageBox behaviour. It's totally unrelated to MessageBox.

Inside a setter RaisePropertyChanged is called no matter of MessageBox result. To correct it you need to add '{' and '}', because only one line of statement is executed after if condition.

See: if-else (C# reference)[^]
Share this answer
BeeDev 17-Nov-14 15:55pm     CRLF
Could you please clarify a little bit more; add the {brackets} around what? The only thing I did is moving RaisePropertyChanged outside the brackets since you said it is called no matter what; however, that didn't change anything, "Set" still being hit 2 times. if (result == MessageBoxResult.Yes) { _SelectedConfig = value; } RaisePropertyChanged();
Maciej Los 17-Nov-14 16:11pm    
Please, be more attentive. The second bracket should be after this line: RaisePropertyChanged() } <--here ;)
BeeDev 17-Nov-14 16:23pm     CRLF
You make me feel pretty dumb :) Here is what I have; however, the issue still there. if (result == MessageBoxResult.Yes) { _SelectedItem = value; RaisePropertyChanged(); }
BeeDev 17-Nov-14 16:58pm    
I ended up handling the logic in Code Behind any way since it is UI related.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900