我需要将文本块(或者也可能是图像 - 无论哪种方式,它是用户控件)的双击事件绑定到我的 ViewModel 中的命令。
TextBlock.InputBindings 似乎没有正确绑定到我的命令,有什么帮助吗?
我需要将文本块(或者也可能是图像 - 无论哪种方式,它是用户控件)的双击事件绑定到我的 ViewModel 中的命令。
TextBlock.InputBindings 似乎没有正确绑定到我的命令,有什么帮助吗?
<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
试试 Marlon Grech附加的命令行为。
很简单,让我们使用 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 应用程序中使用过它
我也有一个类似的问题,我需要将列表视图的 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);
}
这并不简单,但它真的很简单并且有效。