XAML user events on my UserControl on Windows Phone 7

I am doing a UserControl on Windows Phone 7 and I want that when the user clicks the Ok button, the other XAMLs that use my UserControl can add an event related to this.

Using the example, do the following:

I have my MainPage.xaml and I use my UserControl there, so this is something like:

<local:MyUserControl Canvas.Top="0" x:Name="lSelector" Width="480" Height="800" Value="0000"/> 

The value is just the DependencyProperty that I created. I want to be able to do something like this:

 <local:MyUserControl Canvas.Top="0" x:Name="lSelector" Width="480" Height="800" Value="0000" ValueChanged="lSelector_ValueChanged"/> 

How can i do this?

+7
source share
3 answers

Add an event to your UserControl as shown below and it will appear as a regular event

  public partial class UserControl1 : UserControl { public delegate void ValueChangedEventHandler(object sender, EventArgs e); public event ValueChangedEventHandler ValueChanged; public UserControl1() { // Required to initialize variables InitializeComponent(); } private void Button_Click(object sender, System.Windows.RoutedEventArgs e) { if (ValueChanged != null) { ValueChanged(this, EventArgs.Empty); } } } 

Custom event

Then just subscribe to it

  private void UserControl1_ValueChanged(object sender, System.EventArgs e) { // TODO: Add event handler implementation here. } 
+25
source

Try using DependencyPropertyChangedEventHandler.

Those.

  public event DependencyPropertyChangedEventHandler SelectionChanged { add { } remove { } } 

So, you should see a change event in your xaml user control

0
source

Use RoutedEvent :

  public class Callbackable : ContentControl { public static readonly RoutedEvent CommandExecutedEvent = EventManager.RegisterRoutedEvent( "CommandExecuted", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Callbackable)); // Provide CLR accessors for the event public event RoutedEventHandler CommandExecuted { add { AddHandler(CommandExecutedEvent, value); } remove { RemoveHandler(CommandExecutedEvent, value); } } } 

Using:

 <local:Callbackable> <Button local:Callbackable.CommandExecuted="Button_Executed" > Click me </Button> </local:Callbackable> 
-3
source

All Articles