Exposing an Event in a Custom Usercontrol - Window Store Application

I seem to have a problem displaying events in Xaml. I declared a public event handler in a custom user control.

public sealed partial class FoodItemControl : UserControl
{
    public event EventHandler<StringEventArgs> thumbnailClicked;

    public FoodItemControl()
    {
        InitializeComponent();
        (this.Content as FrameworkElement).DataContext = this;
    }



    private void Thumbnail_Tapped(object sender, TappedRoutedEventArgs e)
    {
        var handler = thumbnailClicked;
        if (handler != null)
        {
            handler(this, new StringEventArgs());
        }
    }
}

But when I go to assign an event to it in xaml, the event handler handler cannot be found. I.e

<local:FoodItemControl thumbnailClicked="SOMETHING" />

Am I missing something in the example I found?

EDIT : It would seem that my problem was that I defined the event as EventHandler <StringEventArgs>. It worked as soon as I changed it to just EventHandler Ie

public event EventHandler thumbnailedClicked;

However, I still do not understand why?

+4
source share
1 answer

, :

public sealed partial class FoodItemControl : UserControl
{
    public EventHandler thumbnailClicked
    {
        get { return (EventHandler)GetValue(thumbnailClickedProperty); }
        set { SetValue(thumbnailClickedProperty, value); }
    }

    public static readonly DependencyProperty thumbnailClickedProperty =
  DependencyProperty.Register("thumbnailClicked", typeof(EventHandler),
    typeof(FoodItemControl), new PropertyMetadata(""));


    public FoodItemControl()
    {
        this.InitializeComponent();
        (this.Content as FrameworkElement).DataContext = this;
    }
}
+1

Source: https://habr.com/ru/post/1547659/


All Articles