Assign Click Event to Button in WPF ResourceDictionary

Rezan YILDIZ 1 Reputation point
2022-07-04T11:10:33.987+00:00

Hello,

I keep my theme file (ResourceDictionary) in a separate project. I'm using the following ControlTemplate structure for the DataGrid in my ResourceDictionary:

            <Style TargetType="{x:Type DataGridColumnHeader}">  
  
                <Setter Property="ContentTemplate">  
  
                    <Setter.Value>  
  
                        <DataTemplate>  
  
                            <StackPanel Orientation="Horizontal">  
  
                                <TextBlock Text="{Binding}" Margin="10 5" />  
  
                                <Button x:Name="btnFilter" Content="&#xf0b0;" FontFamily="{StaticResource FontAwesome}" FontSize="16" />  
  
                            </StackPanel>  
  
                        </DataTemplate>  
  
                    </Setter.Value>  
  
                </Setter>  
  
            </Style>  

Where I use DataGrid in the related project, I need to assign a click event to the button named btnFilter above, how can I do this?

Methods like VisualTree do not work very well. Since I have nearly 20 columns in Datagrid, I use horizontal scroll and VisualTree does not see the columns hidden by scroll.

What are the best practices I should follow here?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,635 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
745 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 26,576 Reputation points Microsoft Vendor
    2022-07-05T07:26:21.543+00:00

    Hi,@Rezan YILDIZ . Welcome Microsoft Q&A.

    A ResourceDictionary can have code behind just like Windows etc. so you could add an event handler and call DragMove from there.
    My ResourceDictionary is named Dictionary1.xaml , so i create a new file named Dictionary1.xaml.cs .

    The code behind file should then look like this.
    217613-find.txt

    After that you need to add the x:Class attribute to the Xaml file.

    <ResourceDictionary    x:Class="AssignClickEventtoButtoninResourceDictionary.Dictionary1"  
                 ...>  
        <Style x:Key="column" TargetType="{x:Type DataGridColumnHeader}">  
            <Setter Property="ContentTemplate">  
                <Setter.Value>  
                    <DataTemplate>  
                        <StackPanel Orientation="Horizontal">  
                            <TextBlock x:Name="tb" Text="{Binding}" Margin="10 5" />  
                            <Button x:Name="btnFilter" Content="filter"    Click="btnFilter_Click" FontSize="16" />  
                        </StackPanel>  
                    </DataTemplate>  
                </Setter.Value>  
            </Setter>  
        </Style>  
    </ResourceDictionary>  
    

    App.xaml:

    <Application.Resources>  
            <ResourceDictionary Source="Dictionary1.xaml" />  
        </Application.Resources>  
    

    MainWindow.xaml:

      <Window.DataContext>  
            <local:ViewModel/>  
        </Window.DataContext>  
        <Grid>  
            <DataGrid ItemsSource="{Binding ItemsViewSource.View}" ColumnHeaderStyle="{StaticResource column}" />  
        </Grid>  
    

    The result:

    217623-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments

  2. Rezan YILDIZ 1 Reputation point
    2022-07-05T07:31:28.963+00:00

    First of all, thank you for your answer, but there are some problems in implementing this solution.

    The style file does not allow me to assign a click event and I cannot access the btnFilter event anyway because my style file is in another project (assembly).

    I would be grateful if you could help with these considerations.