1. The first and right way:
(If you know about the MVVM pattern), it will control for you, say MyControl , to open a DependencyProperty of type ICommand , named for example. MyControlButtonClickCommand.
Xaml:
<UserControl> <Button Command={Binding MyControlButtonClickCommand, Source={RelativeSource Self}} /> </UserControl>
Code-Behind:
public ICommand MyControlButtonClickCommand { get { return (ICommand)GetValue(MyControlButtonClickCommandProperty); } set { SetValue(MyControlButtonClickCommandProperty, value); } } public static readonly DependencyProperty MyControlButtonClickCommandProperty = DependencyProperty.Register("MyControlButtonClickCommand", typeof(ICommand), typeof(MyControl), new PropertyMetadata(null));
You use UserControl as follows:
<phone:PhoneApplicationPage> <namespace:MyControl MyControlButtonClickCommand="{Binding ControlButtonCommand}" /> </phone:PhoneApplicationPage>
Where ControlButtonCommand is a ViewModel property (your custom object) living in the DataContext of your Page .
2. There is also a simpler and more dirty way with which I dissuade you:
Just as you set the MyControlButtonClickCommand dependency MyControlButtonClickCommand and, instead of exposing it, you can set the MyControlButtonClick event and subscribe to it on the MyControlButtonClick page. Inside your UserControl code, you must subscribe to the Click button and fire your own MyControlButtonClick event.
Hope this helps you.
source share