How to wire up a click event for a custom usercontrol button? Should I use CustomControl?

后端 未结 2 1782
青春惊慌失措
青春惊慌失措 2021-02-20 14:57

I wanted to create a button that had an image and a textblock as content. So I went about looking for an answer and found a post (Reusable Custom Content for Buttons) w

2条回答
  •  滥情空心
    2021-02-20 15:19

    The easiest option would be to just make your UserControl expose a click event, and pass through your Button's click event.

    In MyButton's xaml:

    In MyButton's code:

    public event RoutedEventHandler Click;
    
    void onButtonClick(object sender, RoutedEventArgs e)
    {
        if (this.Click != null)
        {
            this.Click(this, e);
        }
    }
    

    You can then leave your "implementation" code as-is.

提交回复
热议问题