WPF如何将mousedown(命令/动作)绑定到标签 [英] WPF how to bind mousedown (command/action) to label
问题描述
我可以找到很多命令鼠标绑定到一个按钮,但是如果我想将一个 mousedown 事件绑定到一个绑定(MVVM 模式)呢?我找不到答案,可能是我看不到的很小的问题,但有人可以帮我解决这个问题吗?
I can find a lot of command mouse bindings to a button, but what if I want to bind a mousedown event to a binding (MVVM pattern)? I can't find the answer, probably it is something very small I don't see but can someone help me with this?
xml:
<DataTemplate>
<Grid AllowDrop="True">
<Rectangle Width="100" Height="50" RadiusX="4" RadiusY="4" Fill="LightBlue"/>
<Label Content="{Binding EntityName}" MouseDown="{Binding DoSomething}"/>
</Grid>
</DataTemplate>
推荐答案
您可以使用交互触发器:
You could use an interaction trigger:
<Label Content="{Binding EntityName}" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown" >
<i:InvokeCommandAction Command="{Binding DoSomething}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Label>
请参阅以下博文了解更多信息:https://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/
Please refer to the following blog post for more information: https://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/
您需要添加对 System.Windows.Interactivity 的引用.dll.
更新版本(上面提到的包不是库作者制作的):
Updated version (package mentioned above is not produced by library author):
- 安装 Microsoft.Xaml.Behaviors.Wpf NuGet 包.
- 添加对 http://schemas.microsoft.com/xaml/behaviors 的 xaml 引用
- Install the Microsoft.Xaml.Behaviors.Wpf NuGet package.
- add xaml reference to http://schemas.microsoft.com/xaml/behaviors
请参阅 https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/ 了解更多详情
See https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/ for more details
这篇关于WPF如何将mousedown(命令/动作)绑定到标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!