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"/>
Here's the most minimalistic solution, maybe it can be expanded but right now too tired for this :)
Name the inner button in your xaml to be able to access it
<Button Name="innerButton" Style="{DynamicResource CloseButtonStyle}"/>
Then expose the event like this
public partial class CloseButton : UserControl
{
public event EventHandler Click;
public CloseButton()
{
InitializeComponent();
innerButton.Click += ButtonClick;
}
private void ButtonClick(object sender, RoutedEventArgs e)
{
var eventHandler = this.Click;
if (eventHandler != null)
{
eventHandler(this, e);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.