如何在DataTemplate按钮中同时使用Command和EventTrigger?
我在 DataTemplate 中有一个按钮,它绑定到我的 ViewModel 中的命令。该按钮还有一个 EventTrigger,它启动隐藏编辑控件(该按钮是其中一部分)的 Storyboard。
如果我拾取 PreviewMouseDown 事件,Storyboard 工作正常,但该命令永远不会被调用。如果我在 EventTrigger 中拾取 MouseDown 事件,则命令将起作用,但 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>
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.)
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.
How can I get both the Command and the Storyboard to execute when the button is clicked?
<Button Content="Save" Command="{Binding SaveCommand}" >
<Button.Triggers>
<EventTrigger RoutedEvent="Button.PreviewMouseDown">
<EventTrigger.Actions>
<BeginStoryboard Storyboard="{DynamicResource sbCloseTitleEdit}"/>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最终通过为包含 DataTemplate 的 ResourceDictionary 添加代码隐藏文件来不优雅地解决了我的问题。我不知道这是可能的,但找到了这个解释:
Is it possible to set code Behind a Resource Dictionary in WPF for event Handling?< /a>
我没有使用 EventTrigger 来开始隐藏编辑控件的 Storyboard,而是向代码隐藏添加了 Click 处理程序。这有点混乱,因为我必须相对于单击的按钮定位面板,因为这是唯一可用的参考,但它有效。
如果有人有的话,仍在寻找更好的解决方案。
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:
Is it possible to set code behind a resource dictionary in WPF for event handling?
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.
我试过你的代码,我发现它与
PreviewMouseDown
上的事件触发器一起工作正常,只是先执行命令,然后触发动画。这是我的资源、
我的 xaml
和我的视图模型,
您确定没有错过什么吗?
Ive tried your code, and I find that it works OK with the event trigger on
PreviewMouseDown
, its just that the command is executed first, then the animation fires.Heres my resources
my xaml
and my view model
are you sure you havent missed something ?
我通过调用 eventtrigger 中的命令以及要触发的其他操作来实现此目的:
I achieved this by invoking the command within the eventtrigger along with the other action you want to trigger: