【问题标题】:How to bind a command in WPF to a double click event handler of a control?如何将 WPF 中的命令绑定到控件的双击事件处理程序?
【发布时间】:2010-11-20 13:56:51
【问题描述】:

我需要将文本块(或者也可能是图像 - 无论哪种方式,它是用户控件)的双击事件绑定到我的 ViewModel 中的命令。

TextBlock.InputBindings 似乎没有正确绑定到我的命令,有什么帮助吗?

【问题讨论】:

    标签: wpf data-binding mvvm command double-click


    【解决方案1】:
    <Button>
    <Button.InputBindings>
    <MouseBinding Gesture="LeftDoubleClick" Command="YourCommand" />
    </Button.InputBindings>
    </Button>
    

    http://thejoyofcode.com/Invoking_a_Command_on_a_Double_Click_or_other_Mouse_Gesture.aspx

    【讨论】:

    • +1 就我个人而言,我认为这个答案应该是正确的。它不仅是迄今为止最简单的,而且还可以通过绑定到视图模型中的ICommand 与 MVVM 一起使用:&lt;MouseBinding Gesture="LeftDoubleClick" Command="{Binding YourCommand}" /&gt;
    • 不工作(可能只在某些对象上),直到你做了这个:telerik.com/community/forums/wpf/gridview/…
    • 凌乱的附加行为的好选择:)
    • 这真是一整袋真棒!它甚至适用于开箱即用的其他控件。例如
    • 但是,您似乎不能在样式中使用它。
    【解决方案2】:

    试试 Marlon Grech 的 attached command behaviors

    【讨论】:

    • 我的项目中已经有了三个助手类。我只是希望 WPF 能够完全支持开发人员想要用它做的所有这些事情,但我想这会及时到来,嘿。谢谢,这工作:)
    • 我同意你的普遍看法 - 对 MVVM 的支持并没有更多地融入 WPF,这有点令人沮丧。大多数经验丰富的 WPF 开发人员都建立了自己的小型辅助帮助程序库。如果您使用 Silverlight,功能上的差距会更大!
    • 不要单独链接到外部网站 - 请包括答案。如果该网站消失了,这将变得毫无用处。
    【解决方案3】:

    很简单,让我们使用 MVVM 方式: 我在这里使用的是易于学习且功能强大的 MVVM Light。

    1. 将以下行放入 xmlns 声明:

    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"  
    xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;
                                       assembly=GalaSoft.MvvmLight.Extras.WPF4"
    

    2. 像这样定义你的文本块:

    <textBlock text="Text with event">
       <i:Interaction.Triggers>
          <i:EventTrigger EventName="MouseDoubleClick">
             <GalaSoft_MvvmLight_Command:EventToCommand 
                                 Command="{Binding Edit_Command}"/>
          </i:EventTrigger>
       </i:Interaction.Triggers>
    </textBlock>
    

    3.然后在视图模型中编写命令代码!!!

    ViewModel1.cs

    Public RelayCommand Edit_Command
    {
       get;
       private set;
    }
    
    Public ViewModel1()
    {
       Edit_Command=new RelayCommand(()=>execute_me());
    }
    
    public void execute_me()
    {
       //write your code here
    }
    

    我希望这对你有用,因为我在 Real ERP 应用程序中使用过它

    【讨论】:

    • 这些'gala-soft-stuff'有点乱,但交互方法很好!
    【解决方案4】:

    我也遇到了类似的问题,我需要将列表视图的 MouseDoubleClick 事件绑定到我的 ViewModel 中的命令。

    我想出的最简单的解决方案是放置一个具有所需命令绑定的虚拟按钮,并在 MouseDoubleClick 事件的事件处理程序中调用按钮命令的 Execute 方法。

    .xaml

     <Button Visibility="Collapsed" Name="doubleClickButton" Command="{Binding Path=CommandShowCompanyCards}"></Button>
                    <ListView  MouseDoubleClick="ListView_MouseDoubleClick" SelectedItem="{Binding Path=SelectedCompany, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Margin="0,10,0,0" ItemsSource="{Binding Path=CompanyList, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" HorizontalContentAlignment="Stretch" >
    

    代码隐藏

         private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
                {
                    doubleClickButton.Command.Execute(null);
                }
    

    这并不简单,但它真的很简单,而且很有效。

    【讨论】:

      猜你喜欢
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 2012-06-11
      • 2014-11-06
      • 2021-04-21
      • 2011-06-21
      • 2011-10-11
      • 1970-01-01
      相关资源
      最近更新 更多