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


I wanted to create a button that contained an image and had a text block in the form of the material so I wanted to find an answer Told and found a post ( ) that asked me to create a user control.

I did this and it works great, I can set text through image source and dependency properties. However, I'm stuck because there is no click event for my control.

I dig a bit further and concluded that maybe I need a custom button-generated custom control. is this correct? Or would it be better to wire one click event on my usertrinstr?

Here's my User Control:

  & lt; UserControl x: Class = "Client.Usercontrols.MyButton" Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/ 2006 / xaml "Minhite =" 30 "minwid =" 40 "datacentext =" {binding relative saras = {relative escrows}} "& gt; & Lt; Button width = "auto" horizontal align = "center" & gt; & Lt; Border Corner Radius = "5" Borderline = "1" Borderbrush = "Transparent" & gt; & Lt; Grid & gt; & Lt; Image name = "tehImage" source = "{binding image source}" /> & Lt; TextBlockName = "tehText" text = "{binding text}" style = "{DynamicResource ButtonText}" /> & Lt; / Grid & gt; & Lt; / Border & gt; & Lt; / Button & gt; & Lt; / UserControl & gt;  

Implementation

  & lt; My: MyButton ImageSource = "../ Images / Main Sync Button." "ImageWidth =" 141 "text =" Synchronize "=" btn synchronize_link "/>  

The easiest option is to simply expose a click event to your user control, and pass through the click event of your button.

In MyButton's xaml:

  & gt; Button width = "Auto" horizontal alignment = "center" click = "on button">  

In the code of MyButton:

  click the public event RoutedEventHandler Button ButtonClick (Object Sender, RoutedEventArgs e) {if.Click (this, e);}}  

You can use your "execution" You can leave the code.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -