开发者

How to handle Wpf DataGrid CellEditEnding event in MVVM?

MVVM doesn't allow code behind and so even开发者_如何学Got handling. So what's the MVVM way to be notifyed that a cell been changed ?


Its a pretty legit and obvious question since there are pretty legit solutions to the problem for MVVM. I guess someone marked it down because this has probably been asked countless times before.

I know there is something built in to do this in .Net4. I am stuck at .Net3.5 so I use the CommandReference class from the WPF toolkit, to convert an event in the view to a command in a viewmodel.


Do you need to handle the CellEditEnding Event? Would it be a possibility to execute the code in the setter of the property which is bound to the cell?


"MVVM doesn't allow code behind and so event handling."

Whoops! There's the issue.

MVVM discourages code behind, it's true, but only when something can be done reasonably via Xaml. "Thou shalt not code-behind" has never been said by anyone with a clue.

Sometimes handling an event is the simplest, best way to accomplish your goal. When that's the situation, embrace it, solve your problem, and move on. Sometimes it isn't even possible to accomplish your goal in XAML. In those cases, throw the view-logic in the view's code-behind and move on.

It's far more important to maintain the architectural goals of MVVM (separation of concerns between layers) than it is to strictly abide by rules-of-thumb (such as avoiding code-behind.) If that distinction isn't made, you're missing the forest for the trees.


Since there was never an answer marked to this question; what you want is an "event to command" implementation. Basically, it captures an event of your choice and calls an ICommand implementation on your ViewModel.

Already answered here in summary (check answer by Derek Beattie).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜