0

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"/> 
4
  • Can you show us the code of your control please Commented Oct 29, 2015 at 19:20
  • Its just the xaml that is included with the question right now and i want to avoid adding it in the code. There has to be some way to do it with xaml Commented Oct 29, 2015 at 19:21
  • Its inherited from the UserControl class Commented Oct 29, 2015 at 19:28
  • Yep sorry I was not thinking straight so I removed the comment, will check it out in a minute ;) Commented Oct 29, 2015 at 19:30

1 Answer 1

2

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); } } } 
Sign up to request clarification or add additional context in comments.

3 Comments

Is there no XAML-Only way to do it?
I don't think there is, but even if it exists I imagine it will be very cluttered. By the way if you're thinking MVVM and that it's breaking the pattern (like I was initially when I had to create some controls at work), I wouldn't worry, as you can see here, you're not adding any logic that needs to be in the ViewModel or something like this. You're just exposing the event of inner part of your control.
Works fine. I think i´ll just live with it. Thanks ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.