如何将 WPF 中的命令绑定到控件的双击事件处理程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1293530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:52:19  来源:igfitidea点击:

How to bind a command in WPF to a double click event handler of a control?

wpfdata-bindingmvvmcommanddouble-click

提问by bluebit

I need to bind the double click event of a textblock (or potentially an image as well - either way, its a user control), to a command in my ViewModel.

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

TextBlock.InputBindings does not seem to bind correctly to my commands, any help?

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

采纳答案by Kent Boogaart

Try Marlon Grech's attached command behaviors.

试试 Marlon Grech 的附加命令行为

回答by Legz

<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

http://thejoyofcode.com/Invoking_a_Command_on_a_Double_Click_or_other_Mouse_Gesture.aspx

回答by Adam

it's simple let's use the MVVM way: I'm using here MVVM Light which is easy to learn and strong.

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

1.put the following lines the xmlns declarations :

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.define your textblock just like this:

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.then write your command code in your viewmodel !!!

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

ViewModel1.cs

视图模型1.cs

Public RelayCommand Edit_Command
{
   get;
   private set;
}

Public ViewModel1()
{
   Edit_Command=new RelayCommand(()=>execute_me());
}

public void execute_me()
{
   //write your code here
}

I hope that works for you as I have used it in Real ERP application

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

回答by irem

I also had a similar issue where I needed to bind the MouseDoubleClick event of a listview to a command in my ViewModel.

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

The simplest solution I came up is putting a dummy button which has the desired command binding and calling the Execute method of the button's command in the eventhandler of the MouseDoubleClick event.

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

.xaml

.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" >

codebehind

代码隐藏

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

It is not straightforward but it is really simple and it works.

这并不简单,但它非常简单并且有效。