如何在DataTemplate按钮中同时使用Command和EventTrigger?

3
我在DataTemplate中有一个按钮,它绑定到ViewModel中的一个Command。该按钮还具有一个EventTrigger,触发一个Storyboard来隐藏编辑控件(其中包括该按钮)。
如果我使用PreviewMouseDown事件,Storyboard能够正常工作,但是Command不会被调用。如果我在EventTrigger中使用MouseDown事件,Command可以正常工作,但是Storyboard不会执行。
我该如何使Button被点击时既能执行Command,又能执行Storyboard?
<Button Content="Save" Command="{Binding SaveCommand}" >
    <Button.Triggers>
        <EventTrigger RoutedEvent="Button.PreviewMouseDown">
            <EventTrigger.Actions>
                <BeginStoryboard Storyboard="{DynamicResource sbCloseTitleEdit}"/>
            </EventTrigger.Actions>
        </EventTrigger>
    </Button.Triggers>
</Button>

好问题...我怀疑EventTrigger标记事件为已处理,因此命令从未被调用。不过我也不知道如何解决它... - Thomas Levesque
3个回答

1

我尝试了你的代码,发现在PreviewMouseDown事件触发时它可以正常工作,只是命令先执行,然后才是动画。

这是我的资源。

<Storyboard x:Key="sbCloseTitleEdit">
  <ColorAnimation Storyboard.TargetProperty="(Rectangle.Fill).Color" 
                  To="Blue" Duration="0:0:3" Storyboard.TargetName="rect" >
  </ColorAnimation>
</Storyboard>

我的 XAML

<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;
}

你确定你没有漏掉什么吗?


也许这与我在代码示例中使用DataTemplate有关? - Bill Lefler

1

我最终通过为我的ResourceDictionary添加一个代码后置文件来解决我的问题,该文件包含DataTemplate。我不知道这是可能的,但找到了以下解释:

在WPF中是否可以为资源字典设置代码后置以进行事件处理?

我没有使用EventTrigger来开始隐藏编辑控件的Storyboard,而是向代码后置添加了一个Click处理程序。它有点笨拙,因为我必须相对于单击的按钮定位面板,因为那是唯一可用的参考,但它有效。

如果有更好的解决方案,请告诉我。


0
我是通过在事件触发器中调用该命令以及您想要触发的其他操作来实现这一点的:
<Button Command="{Binding AcceptCommand}" Content="Accept">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="Click">
      <i:InvokeCommandAction Command="{Binding AcceptCommand}"/>
      <triggers:UpdatePropertyAction TargetObject="{Binding RelativeSource={RelativeSource AncestorType={x:Type Controls:ChildWindow}, Mode=FindAncestor}}" TargetProperty="DialogResult" Value="True"  />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</Button>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接