开发者

Custom control wpf, enabling invoking control methods to support MVVM pattern

开发者 https://www.devze.com 2023-04-13 04:26 出处:网络
I am writing a custom control in WPF, and i have a couple of methods in the control that the u开发者_如何学Pythonser of my control can invoke.

I am writing a custom control in WPF, and i have a couple of methods in the control that the u开发者_如何学Pythonser of my control can invoke.

Although it can be invoked directly, due to requests from users, i want to enable the control's methods to be invoked in a MVVM manner.

After research, i take it i have to implement ICommandSource on my control. The MSDN has an example but i find it too much over-engineered for my situation (or maybe because i don;t understand it at all!). All i want is to let my consumers to call my method via a command rather than put their calling code in view's code behind.

Please advise with a simple sample code to implement this. Thanks.


In the Blend SDK is a behavior MethodInvoker (if my memory serves correct) that allows you to bind to a method. That way you do not have to implement ICommand.


If you have access to reflect over the .NET code (or you can download the .NET source code via download) look into how MS does it with ButtonBase. Yes, there is a lot of code under the hood, but that's because if you're going to create a custom control you should account for both scenarios of RoutedEvents and Commands.

0

精彩评论

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

关注公众号