How to prevent selection of a TreeViewItem based on a condition

jama64 picture jama64 · Feb 1, 2011 · Viewed 12.4k times · Source

I have wpf TreeView -- bound to some data. The Treeview resides on the left hand of a window divided into two areas where the tree is the navigation and a panel on the right side changes content depending on the tree node selected.

Not all the nodes of the treeview produce detail information. I want to disable the selection of those nodes. Any idea?

Thanks

Answer

M.C. picture M.C. · Jul 25, 2011

@jama64 : You can achieve what you want if you change the Style from Property IsEnabled to Focusable.

<TreeView.ItemContainerStyle>
     <Style TargetType="{x:Type TreeViewItem}">
         <Setter Property="Focusable" Value="{Binding HasDetails}"/>
     </Style>
</TreeView.ItemContainerStyle>