Handling user control event in its saving page code

I am looking for a solution for the following situation.

In my application, I had the page on which page1 was written, and I placed the user control inside page1. My requirement is that I need to get the button click event used in the user control on page 1. How can I achieve the same in windows phone / silverlight.

+4
source share
3 answers

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.

+14
source

There are two ways to do this. The easiest way would be to double-click the button on the presentation layout.

or

in XML add onCLick = this will pop up a menu to select a new event. click on it and your event for the button click should be there behind the code.

 <button name="b1" onClick="button1_Click()"/> <!--this is what ur XAML will look like --> 

to access the button, press

 private void button1_Click(object sender, RoutedEventArgs e) { // Handle the click event here } 
+1
source

For UserControl, you can create an interface that will be implemented by your Page1.xaml.cs.

 public partial Class SomeControl : UserControl { private OnButtonClick button_click; public interface OnButtonClick { void someMethod(); // generic, you can also use parameters to pass objects!! } // Used to add interface to dynamic controls public void addButtonClickInterface(OnButtonClick button_click) { this.button_click = button_click; } // Buttons UserControlled Click private void ButtonClick(object sender, RoutedEventArgs e) { if(button_click != null) { button_click.someMethod(); } } } 

Here's how to implement and use it.

 public partial class Page1 : PhoneApplicationPage, SomeControl.OnButtonClick { public Page1() { InitializeComponent() // for a new Control SomeControl cntrl = new SomeControl(); cntrl.addButtonClickInterface(this); // or for a control in your xaml someControl.addButtonClickInterface(this); } public void someMethod() { // Here is where your button will trigger!! } } 
0
source

All Articles