Assign Click Event to Button in WPF ResourceDictionary

Rezan YILDIZ 1 Reputation point


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">  
                            <StackPanel Orientation="Horizontal">  
                                <TextBlock Text="{Binding}" Margin="10 5" />  
                                <Button x:Name="btnFilter" Content="&#xf0b0;" FontFamily="{StaticResource FontAwesome}" FontSize="16" />  

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
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

    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.

    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">  
                        <StackPanel Orientation="Horizontal">  
                            <TextBlock x:Name="tb" Text="{Binding}" Margin="10 5" />  
                            <Button x:Name="btnFilter" Content="filter"    Click="btnFilter_Click" FontSize="16" />  


            <ResourceDictionary Source="Dictionary1.xaml" />  


            <DataGrid ItemsSource="{Binding ItemsViewSource.View}" ColumnHeaderStyle="{StaticResource column}" />  

    The result:


    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. 


    0 comments No comments

  2. Rezan YILDIZ 1 Reputation point

    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.