1

TreeView アイテムのダブルクリック イベントをビュー モデルにバインドしようとしています。それは実際に機能しますが、選択されたアイテムを知りたいだけで、SelectedItem へのバインドは機能しません (param は null):

<TreeView CommandBehaviors:MouseDoubleClick.Command="{Binding Connect}" CommandBehaviors:MouseDoubleClick.CommandParameter="{Binding Path=SelectedItem}" 
                              Grid.Column="0" HorizontalAlignment="Stretch" DockPanel.Dock="Left" ItemsSource="{Binding Path=ServerItems, UpdateSourceTrigger=PropertyChanged}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Databases}">
            <TextBlock Text="{Binding}" />
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

コマンドの動作:

public class MouseDoubleClick {
    public static DependencyProperty CommandProperty =
        DependencyProperty.RegisterAttached("Command",
        typeof(ICommand),
        typeof(MouseDoubleClick),
        new UIPropertyMetadata(CommandChanged));

    public static DependencyProperty CommandParameterProperty =
        DependencyProperty.RegisterAttached("CommandParameter",
                                            typeof(object),
                                            typeof(MouseDoubleClick),
                                            new UIPropertyMetadata(null));

    public static void SetCommand(DependencyObject target, ICommand value) {
        target.SetValue(CommandProperty, value);
    }

    public static void SetCommandParameter(DependencyObject target, object value) {
        target.SetValue(CommandParameterProperty, value);
    }
    public static object GetCommandParameter(DependencyObject target) {
        return target.GetValue(CommandParameterProperty);
    }

    private static void CommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) {
        Control control = target as Control;
        if (control != null) {
            if ((e.NewValue != null) && (e.OldValue == null)) {
                control.MouseDoubleClick += OnMouseDoubleClick;
            } else if ((e.NewValue == null) && (e.OldValue != null)) {
                control.MouseDoubleClick -= OnMouseDoubleClick;
            }
        }
    }

    private static void OnMouseDoubleClick(object sender, RoutedEventArgs e) {
        Control control = sender as Control;
        ICommand command = (ICommand)control.GetValue(CommandProperty);
        object commandParameter = control.GetValue(CommandParameterProperty);
        command.Execute(commandParameter);
    }
}

ViewModel の関連部分:

public class MainViewModel : BaseViewModel {
    #region commands
    private ICommand _connect;

    public ICommand Connect {
        get {
            if (_connect == null) {
                _connect = new PGAdmin.Commands.Generic.RelayCommand(param => ConnectToDatabase(param));
            }
            return _connect;
        }
        set {
            _connect = value;
        }
    }
    #endregion

    public void ConnectToDatabase(object param) {
        DebugPopup.Show(param.ToString());
    }
}

もう 1 つの質問 - これを機能させた場合 - param パラメーターには何が入るでしょうか - つまり、監視可能なコレクションの基になるアイテムに何らかの方法でアクセスできますか?

4

1 に答える 1

1

パラメータのバインドが正しくありませんCommandBehaviors:MouseDoubleClick.CommandParameter="{Binding Path=SelectedItem}"。ここでは、ビュー モデルのプロパティにバインドしようとしてSelectedItemいますが、このプロパティは に属していTreeViewます。Visual Studio の [出力] ウィンドウに、対応する Binding エラーが表示されると思います。

このコードを試してみてください:

CommandBehaviors:MouseDoubleClick.CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource Self}}"

ここに関する詳細情報を見つけることができますRelativeSource: WPF の RelativeSources

2 番目の質問について - はい、コレクションからコマンド パラメーターとして下にあるアイテムを受け取ります。

于 2013-05-30T11:27:28.657 に答える