gpt4 book ai didi

WPF如何将mousedown(命令/ Action )绑定(bind)到标签

转载 作者:行者123 更新时间:2023-12-03 10:52:05 26 4
gpt4 key购买 nike

我可以找到很多命令鼠标绑定(bind)到按钮,但是如果我想将 mousedown 事件绑定(bind)到绑定(bind)(MVVM 模式)怎么办?我找不到答案,可能是我看不到的很小的东西,但有人可以帮我解决这个问题吗?

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>

最佳答案

您可以使用交互触发器:

<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/
您需要添加对 System.Windows.Interactivity.dll 的引用.

更新版本(上面提到的包不是库作者制作的):
  • 安装 Microsoft.Xaml.Behaviors.Wpf NuGet 包。
  • 添加对 http://schemas.microsoft.com/xaml/behaviors 的 xaml 引用

  • https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/更多细节

    关于WPF如何将mousedown(命令/ Action )绑定(bind)到标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47199489/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com