Шаг 1
Установите NuGet: Install-Package System.Windows.Interactivity.WPF
Шаг 2
В теге Window добавьте: xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
Шаг 3
В TreeView добавьте:
<TreeView Name="treeView1" ... >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedItemChanged">
<i:InvokeCommandAction Command="{Binding SelectedItemChangedCommand}" CommandParameter="{Binding ElementName=treeView1, Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
...
</TreeView>
Шаг 4
В вашей ViewModel добавьте:
private ICommand _selectedItemChangedCommand;
public ICommand SelectedItemChangedCommand
{
get
{
if (_selectedItemChangedCommand == null)
_selectedItemChangedCommand = new RelayCommand(args => SelectedItemChanged(args));
return _selectedItemChangedCommand;
}
}
private void SelectedItemChanged(object args)
{
//Cast your object
}