CodeLover
CodeLover

Reputation: 95

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

I am writing a custom control in WPF, and i have a couple of methods in the control that the user 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.

Upvotes: 3

Views: 215

Answers (2)

myermian
myermian

Reputation: 32515

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.

Upvotes: 0

Emond
Emond

Reputation: 50672

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.

Upvotes: 1

Related Questions