如何在DataTemplate Button中同时使用Command和EventTrigger? [英] How to use both Command and EventTrigger in DataTemplate Button?

查看:210
本文介绍了如何在DataTemplate Button中同时使用Command和EventTrigger?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DataTemplate中有一个按钮,该按钮绑定到ViewModel中的Command.该按钮还具有一个EventTrigger,可启动故事板,该故事板可隐藏编辑控件(按钮是其中的一部分).

I have a Button in a DataTemplate that is bound to a Command in my ViewModel. The button also has an EventTrigger that kicks off a Storyboard that hides the edit controls (of which the button is a part.)

如果我拾取PreviewMouseDown事件,情节提要板可以正常工作,但Command永远不会被调用.如果我在EventTrigger中拾取MouseDown事件,则该命令有效,但情节提要板无法执行.

The Storyboard works fine if I pick up the PreviewMouseDown event, but the Command never gets called. If I pick up the MouseDown event in my EventTrigger, the Command works, but the Storyboard doesn't execute.

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

<Button Content="Save" Command="{Binding SaveCommand}" >
    <Button.Triggers>
        <EventTrigger RoutedEvent="Button.PreviewMouseDown">
            <EventTrigger.Actions>
                <BeginStoryboard Storyboard="{DynamicResource sbCloseTitleEdit}"/>
            </EventTrigger.Actions>
        </EventTrigger>
    </Button.Triggers>
</Button>

推荐答案

我最终为我的ResourceDictionary添加了一个包含DataTemplate的代码隐藏文件,最终无法很好地解决我的问题.我不知道这是可能的,但找到了以下解释:

I ended up inelegantly solving my problem by adding a code-behind file for my ResourceDictionary that contains the DataTemplate. I wasn't aware that this was possible, but found this explanation:

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

我没有使用EventTrigger来启动故事板来隐藏编辑控件,而是在其背后的代码中添加了一个Click处理程序.这有点麻烦,因为我必须相对于单击的按钮来定位面板,因为这是唯一可用的参考,但是它可以工作.

Instead of using the EventTrigger to begin a Storyboard that hides the edit controls I added a Click handler to the code-behind. It's a bit kludgy as I have to locate the panels relative to the button clicked as that is the only reference available, but it works.

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

Still looking for a better solution if anyone has one.

这篇关于如何在DataTemplate Button中同时使用Command和EventTrigger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆