How to find out if selected by mouse or key? How to find out if selected by mouse or key? wpf wpf

How to find out if selected by mouse or key?


You can handle the OnPreviewKeyDown for your TreeView(or user control having it) and programmatically set a flag in your ViewModel and consider it while refreshing details panel -

protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e){    switch(e.Key)    {        case Key.Up:        case Key.Down:           MyViewModel.IsUserNavigating = true;           break;    }}

A similar approch and other solutions are mentioned in this SO question -

How can I programmatically navigate (not select, but navigate) a WPF TreeView?

Update: [In response to AalanY's comment]

I don't think there is any problem in having some code-behind in Views, that doesn't break MVVM.

In the article, WPF Apps With The Model-View-ViewModel Design Pattern, the author who is Josh Smith says:

In a well-designed MVVM architecture, the codebehind for most Views should be empty, or, at most, only contain code that manipulates the controls and resources contained within that view. Sometimes it is also necessary to write code in a View's codebehind that interacts with a ViewModel object, such as hooking an event or calling a method that would otherwise be very difficult to invoke from the ViewModel itself.

In my experience it's impossible to build an enterprise(of considerable size) application without having any code-behind, specially when you have to use complex controls like TreeView, DataGrid or 3'rd party controls.