1

我正在尝试将 TreeView 项目双击事件绑定到我的视图模型。它实际上有效,只是我想知道选择了哪个项目,并且绑定到 SelectedItem 不起作用(参数为空):

<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());
    }
}

另一个问题 - 如果我完成这项工作 - 我会在 param 参数中得到什么 - 我的意思是 - 我可以以某种方式访问​​我的 observable 集合的底层项目吗?

4

1 回答 1

1

您的参数绑定不正确CommandBehaviors:MouseDoubleClick.CommandParameter="{Binding Path=SelectedItem}"。在这里,您尝试绑定到SelectedItem视图模型的属性,但该属性属于TreeView. 我认为您可以在 Visual Studio 输出窗口中看到相应的绑定错误。

试试这个代码:

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

您可以在此处找到更多信息RelativeSourceWPF 中的相对源

关于您的第二个问题 - 是的,您将从您的收藏中收到您的底层项目作为命令参数。

于 2013-05-30T11:27:28.657 回答