I had the same problem, for some reason it is not possible to do simple an event trigger on attached property in the TreeView object (.net core 3 + wpf).
<i:Interaction.Triggers>
<i:EventTrigger EventName="Expander.Expand">
<i:InvokeCommandAction Command="{Binding MyCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
I had to solve it via custom behavior class.See below for inspiration ;)
public class TreeViewSelectionBehavior : Behavior<TreeView>
{
public object SelectedItem
{
get { return (object)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem", typeof(object), typeof(TreeViewSelectionBehavior), new UIPropertyMetadata(null, OnSelectedItemChanged));
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(TreeViewSelectionBehavior), null);
private static void OnSelectedItemChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var item = e.NewValue as TreeViewItem;
if (item != null)
{
item.SetValue(TreeViewItem.IsSelectedProperty, true);
}
}
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.SelectedItemChanged += OnTreeViewSelectedItemChanged;
this.AssociatedObject.AddHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(this.Expanded));
}
private void Expanded(object sender, RoutedEventArgs e)
{
TreeViewItem item = e.OriginalSource as TreeViewItem;
if (this.Command.CanExecute(item))
{
this.Command.Execute(item);
}
}
protected override void OnDetaching()
{
base.OnDetaching();
if (this.AssociatedObject != null)
{
this.AssociatedObject.SelectedItemChanged -= OnTreeViewSelectedItemChanged;
this.AssociatedObject.RemoveHandler(TreeViewItem.ExpandedEvent, new RoutedEventHandler(this.Expanded));
}
}
private void OnTreeViewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
this.SelectedItem = e.NewValue;
}
}
Then usage is simple
<i:Interaction.Behaviors>
<local:TreeViewSelectionBehavior SelectedItem="{Binding SelectedItem}" Command="{Binding ExpandHistoricalItemCommand}"/>
</i:Interaction.Behaviors>