1

mvvmとPrism2を使用しているときに、ビューでwpfdatagridのMouseDoubleClickイベントをバインドするにはどうすればよいですか。

4

2 に答える 2

2

私は MouseDoubleClickBehaviour を追加することを好み、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}"
 .../>
于 2010-09-07T06:15:22.023 に答える
-1

View のコード ビハインドで MouseDoubleClick イベントをリッスンし、ViewModel で適切なメソッドを呼び出します。

public class MyView : UserControl 
{
    ...

    private MyViewModel ViewModel { get { return DataContext as MyViewModel; } }

    private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        ViewModel.OpenSelectedItem();
    }
于 2010-05-14T10:43:13.170 に答える