Mvvm 视图中的鼠标双击事件
使用mvvm和Prism 2时,如何在视图中绑定wpfdatagrid的MouseDoubleClick事件。在视图的代码隐藏中侦听MouseDoubleClick事件,并在ViewModel上调用适当的方法:Mvvm 视图中的鼠标双击事件,mvvm,binding,wpfdatagrid,prism-2,Mvvm,Binding,Wpfdatagrid,Prism 2,使用mvvm和Prism 2时,如何在视图中绑定wpfdatagrid的MouseDoubleClick事件。在视图的代码隐藏中侦听MouseDoubleClick事件,并在ViewModel上调用适当的方法: public class MyView : UserControl { ... private MyViewModel ViewModel { get { return DataContext as MyViewModel; } } private void
public class MyView : UserControl
{
...
private MyViewModel ViewModel { get { return DataContext as MyViewModel; } }
private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ViewModel.OpenSelectedItem();
}
在视图的代码隐藏中侦听MouseDoubleClick事件,并在ViewModel上调用相应的方法:
public class MyView : UserControl
{
...
private MyViewModel ViewModel { get { return DataContext as MyViewModel; } }
private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ViewModel.OpenSelectedItem();
}
我更喜欢添加一个mousedoubleclickBehavior,然后您可以将其附加到任何控件,该控件将绑定到您的ViewModel。从视图的代码隐藏调用命令会创建我不喜欢的直接依赖项
public static class MouseDoubleClickBehaviour
{
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(MouseDoubleClickBehaviour), new UIPropertyMetadata(null, OnCommandChanged));
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(MouseDoubleClickBehaviour), new UIPropertyMetadata(null));
public static ICommand GetCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(CommandProperty);
}
public static void SetCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(CommandProperty, value);
}
public static object GetCommandParameter(DependencyObject obj)
{
return obj.GetValue(CommandParameterProperty);
}
public static void SetCommandParameter(DependencyObject obj, object value)
{
obj.SetValue(CommandParameterProperty, value);
}
private static void OnCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
{
var grid = target as Selector;
////Selector selector = target as Selector;
if (grid == null)
{
return;
}
grid.MouseDoubleClick += (a, b) => GetCommand(grid).Execute(grid.SelectedItem);
}
}
然后可以在XAML中执行此操作
<ListView ...
behaviours:MouseDoubleClickBehaviour.Command="{Binding Path=ItemSelectedCommand}"
behaviours:MouseDoubleClickBehaviour.CommandParameter="{Binding ElementName=txtValue, Path=Text}"
.../>
我更喜欢添加一个mousedoubleclick行为,然后您可以将其附加到任何控件,该控件将绑定到您的ViewModel。从视图的代码隐藏调用命令会创建我不喜欢的直接依赖项
public static class MouseDoubleClickBehaviour
{
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(MouseDoubleClickBehaviour), new UIPropertyMetadata(null, OnCommandChanged));
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(MouseDoubleClickBehaviour), new UIPropertyMetadata(null));
public static ICommand GetCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(CommandProperty);
}
public static void SetCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(CommandProperty, value);
}
public static object GetCommandParameter(DependencyObject obj)
{
return obj.GetValue(CommandParameterProperty);
}
public static void SetCommandParameter(DependencyObject obj, object value)
{
obj.SetValue(CommandParameterProperty, value);
}
private static void OnCommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
{
var grid = target as Selector;
////Selector selector = target as Selector;
if (grid == null)
{
return;
}
grid.MouseDoubleClick += (a, b) => GetCommand(grid).Execute(grid.SelectedItem);
}
}
然后可以在XAML中执行此操作
<ListView ...
behaviours:MouseDoubleClickBehaviour.Command="{Binding Path=ItemSelectedCommand}"
behaviours:MouseDoubleClickBehaviour.CommandParameter="{Binding ElementName=txtValue, Path=Text}"
.../>
这不符合MVVM模式。这不符合MVVM模式。例如,您可以在文本块上不是选择器鼠标双击的对象上添加处理程序到鼠标双击事件吗?您可以在文本块上不是选择器鼠标双击的对象上添加处理程序到鼠标双击事件吗实例?