开发者

WPF - Making Checked and Unchecked event to execute before actually check or uncheck?

My problem is the fellowing. I have a ListView which is bind to a list of object. Then i have a property in that object that is a boolean. In the ListView i have a checkbox which is set to checked or unchecked depending of the value of the bool of开发者_JS百科 that particuliar object in the list.

So i told myself, why not when you click it perform some action on that row.. but the problem is that the action can be cancelled by the user such showing a windows with both apply and cancel. In the event of the user cancel the action.. the checkbox get checked anyway!

Is there a way to cancel this action ?

Thanks.


As long as you don't have any special processing for when the checkbox is unchecked, then you can just move your special processing to occur when the checkbox is checked rather than firing on when it is clicked. If the cancel is chosen then you can uncheck the checkbox again by resetting the source bool in your object to False, and ignore the unchecked event.

If you do have special processing for unchecked, you can use the binding's SourceUpdated event to detect when the checkbox caused the bool to flip to false rather than the bool causing the checkbox to flip to unchecked.

If you want to detect when your object's bool caused a check to update:

<CheckBox isChecked="{Binding MyBool, Mode=TwoWay, NotifyOnTargetUpdated=True}" TargetUpdated="MyTargetUpdated"/>

If you want to detect when your checkbox caused your object's bool to update:

<CheckBox isChecked="{Binding MyBool, Mode=TwoWay, NotifyOnSourceUpdated=True}" SourceUpdated="MySourceUpdated"/>

You can do both at the same time, thus detecting changes both ways and acting accordingly, so that you can choose to act only when the data updated or only when the CheckBox updated.


I may be misinterpreting your question but it sounds like what you want is you have a ListView with a CheckBox bound to a property in a ViewModel. When the CheckBox is checked or unchecked, you have an event that performs some cancelable action. When the action is canceled, you want to restore the checkbox value to the value before the original check?

If this is so, in the cancel case, just not the IsChecked property in the code behind or not the property value in your ViewModel.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜