You can use the Expression Blend SDK to invoke commands in response to events in general, however, in my experience, not all events are supported by EventTrigger.
For example, this works:
<Label Content="LabelText"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseMove"> <i:InvokeCommandAction Command="{Binding IsVisibleChangedCommand, Mode=OneWay}"/> </i:EventTrigger> </i:Interaction.Triggers> </Label>
But this is not so:
<Label Content="LabelText"> <i:Interaction.Triggers> <i:EventTrigger EventName="IsVisibleChanged"> <i:InvokeCommandAction Command="{Binding IsVisibleChangedCommand, Mode=OneWay}"/> </i:EventTrigger> </i:Interaction.Triggers> </Label>
I'm not sure why, but the Blend SDK is not like the IsVisible property. You can do something similar, but using visibility (instead of IsVisible) with PropertyChangedTrigger as follows:
<Label x:Name="label1" Content="LabelText"> <i:Interaction.Triggers> <ei:PropertyChangedTrigger Binding="{Binding Visibility, ElementName=label1}"> <i:InvokeCommandAction Command="{Binding IsVisibleChangedCommand, Mode=OneWay}"/> </ei:PropertyChangedTrigger> </i:Interaction.Triggers> </Label>
BTW - Here are the namespaces:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
In addition, here is another solution to your specific problem that does not require the Blend SDK:
Instead of directly binding the event to the command, you can bind the visibility property of your object to the viewmodel property and execute the command from the property settings tool as follows:
View:
<UserControl x:Class="SampleApp.Views.EventBindingDemoView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="200" Width="200"> <UserControl.Resources> <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> </UserControl.Resources> <Grid> <Label Content="LabelText" Visibility="{Binding Path=ElementIsVisible, Mode=TwoWay, Converter={StaticResource BooleanToVisibilityConverter}}"/> </Grid> </UserControl>
ViewModel (ViewModelBase should implement INotifyPropertyChanged and OnPropertyChanged (string propertyName)):
public class EventBindingDemoViewModel : ViewModelBase { private bool ElementIsVisibleField = true;
In any case, this should work.