Create dependency properties to configure custom EventHandlers in XAML

I want to add custom event handlers to the default frame elements using DependencyProperties.

Something like the following:

<Border custom:MyProps.HandleMyEvent="someHandler">...</Border>

Here is the code for the control that contains the Border element:

public class MyPage : Page{
    public void someHandler(object sender, EventArgs e){
        //do something
    }
}

Here is an example of how I present a class that defines a property:

public class MyProps{
    public event EventHandler MyInternalHandler;
    public static readonly DependencyProperty HandleMyEventProperty = ...
    public void SetHandleMyEvent(object sender, EventHandler e){
         MyInternalHandler += e;
    }
}

The problem is that I do not know / did not find hints how to combine DependencyPropertieswith events / delegates and EventHandlers.

Do you have a key?

+5
source share
1 answer

I assume this has nothing to do with WPF, this is a Silverlight question.

, Event . Properties, -, .

, , .

, : -

public class MyEventer
{
    public event EventHandler MyEvent;

    // What would call this??
    protected void OnMyEvent(EventArgs e)
    {
      if (MyEvent != null)
         MyEvent(this, e);
    }
}

, MyEventer , .

public static class MyProps
{

  public static MyEventer GetEventer(DependencyObject obj)
  {
     return (MyEventer)obj.GetValue(EventerProperty );
  }

  public static void SetEventer(DependencyObject obj, MyEventer value)
  {
    obj.SetValue(EventerProperty , value);
  }

  public static readonly DependencyProperty EventerProperty =
        DepencencyProperty.RegisterAttached("Eventer", typeof(MyEventer), typeof(MyProps), null)

  }
}

: -

<Border ...>
   <custom:MyProps.Eventer>
      <custom:MyEventer MyEvent="someHandler" />
   </custom:MyProps.Eventer>
</Border>

xaml, , Visual Studio .

, : ?

+5

All Articles