绑定到TreeView所选项目

时间:2013-05-30 09:53:13

标签: c# wpf mvvm treeview

我正在尝试将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());
    }
}

另一个问题 - 如果我做这个工作 - 我会在param参数中得到什么 - 我的意思是 - 我能以某种方式到达我的可观察集合的基础项吗?

1 个答案:

答案 0 :(得分:1)

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

试试这段代码:

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

您可以在此处找到有关RelativeSource的更多信息:RelativeSources in WPF

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