I have a WPF User Control including a button and a template. What do I have to do to add an Click event to that button?
CloseButton.xaml:
<UserControl x:Class="Toolbox.UserContols.CloseButton" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:Toolbox.UserContols" mc:Ignorable="d" d:DesignHeight="10" d:DesignWidth="10"> <UserControl.Resources> <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/> <ImageBrush x:Key="Button.Static.Background" ImageSource="/Toolbox.Resources;component/Resources/CloseButton.png" Opacity="0.5"/> <ImageBrush x:Key="Button.MouseOver.Background" ImageSource="/Toolbox.Resources;component/Resources/CloseButton.png" Opacity="1"/> <Style x:Key="CloseButtonStyle" TargetType="{x:Type Button}"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true" Opacity="0.5"> <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="Background" Value="{StaticResource Button.MouseOver.Background}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources> <Button Style="{DynamicResource CloseButtonStyle}"/> </UserControl> Use of CloseButton:
<UserContols:CloseButton x:Name="close" Margin="10" HorizontalAlignment="Right" VerticalAlignment="Top"/> Now I want to add a Click event like I do on a normal button but my UserControl doesn´t seem to have the onClick option.
Example Code:
<UserContols:CloseButton x:Name="close" Click="close_onClick" Margin="10" HorizontalAlignment="Right" VerticalAlignment="Top"/>