Click here to Skip to main content
15,912,932 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a custom WPF user control that is intended for use in a TreeView. I would like to know how I can add a Selected method similar to that of the TreeVieeItem. My end goal it to keep the user control from being selected when its enabled property is set to false.
Posted

1 solution

You cannot, by why simple reason: there is no such method as Selected in the class System.Windows.Controls.TreeViewItem:
http://msdn.microsoft.com/en-us/library/system.windows.controls.treeviewitem%28v=vs.110%29.aspx[^].

Moreover, such method could not appear in the code of anyone knowing English and understanding reasonable naming conventions. There is an event with this name though. The whole problem is unclear. Selection is the property of the TreeView, not your control.

Normally, any tree node can be selected. Making some nodes unselectable can be a pretty difficult work. You would rather need to create a derived class of the tree view. You would need to handle a number of events which would detect an attempt to select some node, determine if it can be selected or not, and if it is not, some nearby node should be selected instead. Say, if you select nodes by up and down arrow keys, selection should "jump" over the unselectable node. If you are moving up, and the node on top is unselectable one, you would need to cancel the event and its further propagation, same thing about moving down. Something like that should happen on the click on the unselectable node itself. This would be a good deal of research and development. Nothing impossible though. :-)

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900