After a lot of investigation I have found the following.
If event is fired from the control located in the Window.Resources
it won't be caught by the blend no matter what.
The only option to make it work is to register this event in the MainWindow.cs .
Here is the code. The event is never fired if clicked on the button
<pre lang="xml"><Window x:Class="Tac.MainWindow"
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:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:tac="clr-namespace:Tac"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" >
<i:Interaction.Triggers>
<!--This never works no matter what-->
<i:EventTrigger EventName="FilterLoaded" SourceName="MyBtn">
<tac:ExecuteCommandAction Command="{Binding FilterLoadedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Window.Resources>
<Style x:Key="DataGridHeaderFilterControl" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
<tac:MyButton Height="25" Content="Click" x:Name="MyBtn"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<DataGrid x:Name="dataGrid1" IsReadOnly="True" SelectionMode="Extended"
ColumnHeaderStyle="{StaticResource DataGridHeaderFilterControl}"
ItemsSource="{Binding ItemsSource, Mode=OneWay}">
</DataGrid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Collections;
namespace Tac
{
public partial class MainWindow : Window
{
DataTable dt = new DataTable();
public RelayCommand<object> FilterLoadedCommand { get; private set; }
public MainWindow()
{
InitializeComponent();
FilterLoadedCommand = new RelayCommand<object>(param => FilterLoadedChanged());
DataContext = this;
dt.Columns.Add("Vendor", typeof(string));
for (int i = 0; i < 5; i++)
{
dt.Rows.Add(Convert.ToChar(i + 65));
}
this.dataGrid1.ItemsSource = new DataView(dt);
}
void FilterLoadedChanged()
{
}
}
}
<pre lang="cs">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Collections;
namespace Tac
{
class MyButton : Button
{
public static readonly RoutedEvent FilterLoadedEvent = EventManager.RegisterRoutedEvent(
"FilterLoaded", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MyButton));
public event RoutedEventHandler FilterLoaded
{
add { AddHandler(FilterLoadedEvent, value); }
remove { RemoveHandler(FilterLoadedEvent, value); }
}
protected override void OnClick()
{
base.OnClick();
RaiseEvent(new RoutedEventArgs(MyButton.FilterLoadedEvent));
}
}
}
Now if I would add the following code in the MainWindow
<pre lang="cs">public static readonly RoutedEvent FilterLoadedEvent = EventManager.RegisterRoutedEvent(
"FilterLoaded", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MainWindow));
public event RoutedEventHandler FilterLoaded
{
add { AddHandler(FilterLoadedEvent, value); }
remove { RemoveHandler(FilterLoadedEvent, value); }
}
And fire the event from the MyButton as:
RaiseEvent(new RoutedEventArgs(MainWindow.FilterLoadedEvent));
it would all work fine.
Any idea how to fix it?
Thank you