7

I'm making a UserControl in Windows Phone 7 and i want that when the user clicks on an Ok button the other XAMLs which are using my UserControl can be able to add an event related to that.

Using an example it's like this:

I have my MainPage.xaml and i'm using my UserControl there, so it's something like:

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

Value is just a DependencyProperty that i created. What i want is 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 that?

2 Answers 2

25

Add event to your UserControl like code below and it will appears like normal 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.
    }
0

Try using DependencyPropertyChangedEventHandler.

Ie.

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

So you should see the selection changed event in your custom xaml control

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.