我试过你的代码,我发现它在事件触发器上工作正常PreviewMouseDown
,只是先执行命令,然后触发动画。
这是我的资源
<Storyboard x:Key="sbCloseTitleEdit">
<ColorAnimation Storyboard.TargetProperty="(Rectangle.Fill).Color"
To="Blue" Duration="0:0:3" Storyboard.TargetName="rect" >
</ColorAnimation>
</Storyboard>
我的 xml
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Content="Save" Command="{Binding SaveCommand}" >
<Button.Triggers>
<EventTrigger RoutedEvent="Button.PreviewMouseDown">
<EventTrigger.Actions>
<BeginStoryboard
Storyboard="{StaticResource sbCloseTitleEdit}"/>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
<Rectangle Name="rect" Width="30" Height="30"
Grid.Column="1" Fill="Red" />
</Grid>
和我的视图模型
public class MainViewModel
{
public ActionCommand SaveCommand { get; private set; }
public MainViewModel()
{
SaveCommand = new ActionCommand();
}
}
public class ActionCommand : ICommand
{
public void Execute(object parameter)
{
// gets fired if event trigger is preview mode
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
}
你确定你没有错过什么吗?