1
votes

I'm trying to bind TreeView item double click event to my view model. It actually works, only I would like to know which item was selected, and binding to SelectedItem doesn't work (param is 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>

Command behaviors:

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

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

Another question - if I make this work - what would I get in the param parameter - I mean - can I somehow get to the underlying item of my observable collection?

1

1 Answers

1
votes

Your binding of parameter is incorrect CommandBehaviors:MouseDoubleClick.CommandParameter="{Binding Path=SelectedItem}". Here you trying to bind to SelectedItem property of your view model but this property belongs to TreeView. I think you can see corresponding Binding error in your Visual Studio Output window.

Just try this code:

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

You can find more information about RelativeSource here: RelativeSources in WPF

Regards to your second question - yes, you will receive your underlaying item from your collection as command parameter.