I have a Button inside a UserControl:
<UserControl.Resources>
<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
<ContentPresenter />
</ControlTemplate>
</UserControl.Resources>
<Button Template="{StaticResource ButtonTemplate}" Click="Button_Click" />
But, if I specify a Template, this Button does not fire the Click event.
Why? How can I solve this problem?
The code-behind:
public event RoutedEventHandler Click;
private void Button_Click(object sender, RoutedEventArgs e)
{
if (Click != null)
Click(sender, e);
}
Your button template is a simple ContentPresenter. In the code you give us, you put nothing in the button so it won’t have any size. It will be impossible to click.
This code works and if you click on “test”, the Button’s Click event is correctly triggered. I did it in a window but it’s the same in a UserControl.
If you are talking about your custom Click Event, it will be fired only if you attached an handler on it.