blu*_*bit 62 data-binding wpf command double-click mvvm
我需要将文本块的双击事件(或者可能是图像 - 无论哪种方式,它的用户控件)绑定到我的ViewModel中的命令.
TextBlock.InputBindings似乎没有正确绑定到我的命令,任何帮助?
小智 245
<Button>
<Button.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="YourCommand" />
</Button.InputBindings>
</Button>
Run Code Online (Sandbox Code Playgroud)
http://thejoyofcode.com/Invoking_a_Command_on_a_Double_Click_or_other_Mouse_Gesture.aspx
这很简单,让我们使用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"
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
3.然后在viewmodel中编写命令代码!!!
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
}
Run Code Online (Sandbox Code Playgroud)
我希望这对你有用,因为我在Real ERP应用程序中使用它
小智 2
我也遇到了类似的问题,我需要将列表视图的 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" >
Run Code Online (Sandbox Code Playgroud)
代码隐藏
private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
doubleClickButton.Command.Execute(null);
}
Run Code Online (Sandbox Code Playgroud)
它并不简单,但非常简单并且有效。
| 归档时间: |
|
| 查看次数: |
66999 次 |
| 最近记录: |