0

I'm currently creating a windows style and I have integrate the button inside. How can I handle the click event of the buttons ? Am I obliged to create a custumcontrol ?

Here is my wpf style :

<Style TargetType="Window" x:Key="borderless">
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="ResizeMode" Value="CanResizeWithGrip"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="Window">
            <Border Style="{DynamicResource windowBorder}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <Button Grid.Column="3" Style="{DynamicResource minimizeButton}"/>
                        <Button Grid.Column="4" Style="{DynamicResource closeButton}"/>
                        <Image Height="20" Width="{Binding Height, RelativeSource={RelativeSource Self}}" Margin="2"/>
                        <TextBlock Text="Title" Style="{DynamicResource windowTitle}" Grid.Column="1"/>
                    </Grid>
                    <ContentPresenter Grid.Row="1"/>
                </Grid>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>

Thank you !

Thibaud
  • 377
  • 1
  • 2
  • 15
  • If you want to adhere to MVVM pattern, I'd suggest binding the `Command` property on your button to an `ICommand` in your view model. This can be done in a `Style`. – Adwaenyth Aug 02 '17 at 09:30
  • Where is the style defined and where do you want to handle the click? Do you need the event parameters or is it just about executing some code (possibly use `Command` instead of the `Click` event) – grek40 Aug 02 '17 at 09:30
  • @grek40 I just want to add close or minimize event to my button. The styles are define in a Dictionnary, not directly in the wpf form. – Thibaud Aug 02 '17 at 09:32
  • At some point I discovered that you can even create a code behind file for resource dictionaries, then the resource can contain a click handler... however, that doesn't appear to be a very common approach. If the functionality is business related, use viewmodel commands, for purely view related functionality, use separate commands. Possibly have a look at `ApplicationCommands` with `CommandBindings` – grek40 Aug 02 '17 at 10:12
  • I'll take a look at the Command as you mentionned. If you have any usefull link in mind ? – Thibaud Aug 02 '17 at 11:36

1 Answers1

1

There are different ways to handle a button click from inside a style in a resource dictionary.

Method 1: code behind file

See: Is it possible to set code behind a resource dictionary in WPF for event handling?

In short:

  • For a resource dictionary file MyResource.xaml, create a code file MyResource.xaml.cs
  • In the xaml file: <ResourceDictionary x:Class="MyNamespace.MyDictionary"
  • In the xaml.cs file, create the specialized resource dictionary as partial class:

.

namespace MyNamespace
{
    public partial class MyDictionary : ResourceDictionary
    {
        public MyDictionary()
        {
            InitializeComponent();
        }
    }
}
  • Add the click event handler the same way you would do it in a window or usercontrol

Method 2: using Command from viewmodel

Just create some ICommand property in the viewmodel and use the Execute method to handle the click action. Then bind the command in xaml

<Button Grid.Column="4" Style="{DynamicResource closeButton}" Command="{Binding MyViewmodelCommand}"/>

Method 3: commands with command binding

Use view commands with command binding. For example I use the ApplicationCommands.Close command, but the principle is the same for self-defined custom commands.

In the style, just use the command:

<Button Grid.Column="4" Style="{DynamicResource closeButton}" Command="ApplicationCommands.Close"/>

Somewhere up the tree where code behind is available, bind the command to an event handler. For example in the window (but I'm not 100% sure if it works for a style that targets the window itself)

<Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Close" Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute"/>
</Window.CommandBindings>
grek40
  • 13,113
  • 1
  • 24
  • 50