【问题标题】:How to use both Command and EventTrigger in DataTemplate Button?如何在 DataTemplate Button 中同时使用 Command 和 EventTrigger?
【发布时间】:2010-10-11 17:27:01
【问题描述】:

我在 DataTemplate 中有一个按钮,它绑定到我的 ViewModel 中的命令。该按钮还有一个 EventTrigger,它启动一个隐藏编辑控件(按钮是其中一部分)的 Storyboard。

如果我选择 PreviewMouseDown 事件,情节提要可以正常工作,但命令永远不会被调用。如果我在 EventTrigger 中选择 MouseDown 事件,则命令有效,但情节提要不执行。

如何在单击按钮时同时执行命令和情节提要?

<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 将事件标记为已处理,因此永远不会调用该命令。虽然不知道如何解决它......

标签: wpf xaml command datatemplate eventtrigger


【解决方案1】:

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

Is it possible to set code behind a resource dictionary in WPF for event handling?

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

如果有人有的话,仍在寻找更好的解决方案。

【讨论】:

    【解决方案2】:

    我试过你的代码,我发现它与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 有关?
    【解决方案3】:

    我通过调用 eventtrigger 中的命令以及您要触发的其他操作来实现这一点:

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

    【讨论】:

      猜你喜欢
      • 2021-05-02
      • 2017-10-04
      • 2014-02-21
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 2019-12-27
      相关资源
      最近更新 更多