开发者

Databinding event handlers in WPF / XAML

开发者 https://www.devze.com 2023-02-09 05:59 出处:网络
I can\'t find any information online about this.Is it even possible to use databinding for event handlers, like we do for Buttons (via Command)?If not, can anyone explain why this 开发者_开发技巧is th

I can't find any information online about this. Is it even possible to use databinding for event handlers, like we do for Buttons (via Command)? If not, can anyone explain why this 开发者_开发技巧is the case? I believe I am lacking some fundamental understanding about databinding, and that's why I can't figure this out on my own.

Ultimately, I've got a DataGrid that displays information from my database. I can easily make changes by implementing an event handler for CurrentCellChanged. However, I'd rather use databinding to preserve the MVVMness of my application.


This functionality does not exist in WPF directly. However, there have been many solutions written to address this lack.

Typically, this is handled via using some form of attached property. The attached property subscribes to the event, and calls the ICommand for you.

For example, MVVM Light includes an EventToCommand Behavior that allows:

you to bind any event of any UI element to an ICommand, for example on the ViewModel, directly in XAML. This makes using Commands much easier, without writing code behind. With the newest version, you can even get the EventArgs of the fired event directly in the ViewModel to handle it.

0

精彩评论

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