A colleague and I came up with a solution to this problem. I wanted to use EventTriggers to handle the Legend Refresh event in my ViewModel, the problem is that EventTriggers do not support passing EventArgs. That is another topic all together, why event args are not passed with the EventTrigger. Anyway, my colleague extended the EventTrigger so that it supports passing the event args and it works great.
I will attempt to explain how it all comes together. In the view, a trigger is added to the legend control:
Code:
<esri:Legend Map="{Binding ElementName=MyMap}"LayerItemsMode="Tree">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Refreshed" >
<Commands:InvokeCommandActionWithArgs Command="{Binding LegendRefreshedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</esri:Legend>
The key thing to notice here is Commands:InvokeCommandActionWithArgs. This is an extended version of TriggerAction and looks like:
Code:
public class InvokeCommandActionWithArgs : TriggerAction<DependencyObject>
{
public static DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand),
typeof(InvokeCommandActionWithArgs));
public ICommand Command
{
get { return GetValue(CommandProperty) as ICommand; }
set { SetValue(CommandProperty, value); }
}
protected override void Invoke(object parameter)
{
if (Command == null) return;
Command.Execute(parameter);
}
}
Also notice in the view that the command binding for the Triggeraction is Command="{Binding LegendRefreshedCommand}" . LegendRefreshedCommand is defined in our ViewModel which looks something like:
Code:
public class MainWindowViewModel {
public ICommand LegendRefreshedCommand { get; private set; }
public MainWindowViewModel()
{
LegendRefreshedCommand = new RelayCommand<Legend.RefreshedEventArgs>(OnLegendRefresh, null);
}
private void OnLegendRefresh(Legend.RefreshedEventArgs refreshedEventArgs)
{
refreshedEventArgs.LayerItem.IsExpanded = false;
}
}
When the Refreshed event on the legend is bound to the LegendRefreshedCommand on the viewmodel via the extended TriggerAction the OnLegendRefresh method will handle the event. The RefreshedEventArgs are passed in and the LayerItem can now be accessed and the IsExpanded property set to false. This is the piece that is missing when using the plain EventTrigger, because it does not pass the event args.
Hopefully that all makes sense and is easy to follow. I have attached a very simple reference project that might make it more clear.
Bookmarks