wpftreeview

WPF Highlight multiple nodes/leaves in a treeview based on selection


I have a bound, hierarchical TreeView. Do to the nature of the underlying data, the same object may appear multiple times in the tree:

A
--A1
  ---A11
--A2
B
--B1
--A1      <<< A1 is a node under both A and B
  ---A11  <<< A11 also shows up twice since it is a child of A1

Please note that although I've shown 3 levels in my example, the real data can have an unlimited number of levels. And the same object may show up in multiple levels. My TreeView with HierarchicalDataTemplate displays this beautifully.

Since A1 may appear in multiple places, I want to highlight this to the user so that they know that editing A1 will impact not just the A branch but also the B branch.

Please note that the goal is to just HIGHLIGHT the other A1, not change the selection.

The only solution I've arrived at so far is to have an IsHighlighted property in the underlying class for the objects. When A1 is selected I change the value of IsHighlighted through the SelctedItemChanged handler. IsHighlighted is in turn bound to the TreeViewItem's Background property through a converter and a GUI update is triggered by a property change notification.

The problem I have with this method is that I now have a purely UI-driven property in my model. While I'm not following a strict adherence to MVVM, I'd like to at least keep the model a bit more isolated.

What is a suggested way of implementing this?


Solution

  • I would stick with a model with two properties:

    Here's why:

    The logic you are talking about is, in essence, view logic, but it is not deciding how the view is displaying data; instead, it is only representing what the view is displaying. Therefore, it should reside in the view model. View models are agnostic - meaning that they don't have view objects in them (e.g. Listview, Buttons, etc..), but they are an object representation of the view (a.k.a the model of the view).