There are many solutions to tackle this problem with, and while ItemContainerStyle, you make it a lot easier to work with selections and expansion states. Summaryīy creating and implementing a base class for the objects that you wish to use and manipulate within a TreeView, and using the gained properties in the Selection button only works for the top level items). TreeViewItemBase class) in the event handlers for the two test buttons (please bear in mind that, to keep the code as small and simple as possible, the We also use these two properties on the Person objects (inherited from the (Jane Doe) to be initially selected and expanded, as shown on the screenshot. The most interesting part happens when I set the IsExpanded and IsSelected properties on the person2 object. ItemsSource of the TreeView, which, with a bit of help from the defined template, renders them the way they are shown on the screenshot. I add the persons to a list, which I assign as the In the main Window class I simply create a range of persons, while adding children to some of them. The concept of notificationĬhanges are explained in the Data binding chapters. Notify of changes to these two essential properties - without this, selection/expansion changes won't be reflected in the UI. You should be aware that the TreeViewItemBase class implements the INotifyPropert圜hanged interface and uses it to In the code-behind, I have defined a Person class, with a couple of properties, which inherits our extra properties from the TreeViewItemBase class. Might not completely understand that part, but it's simply a matter of tying together the properties on our own custom class with the IsSelected and IsExpanded properties on the TreeViewItems, which is done with Style setters. If you haven't read the chapters on styling yet, you Then we have the TreeView, for which I haveĭefined an ItemTemplate (as demonstrated in a previous chapter) as well as an ItemContainerStyle. I have defined a couple of buttons to be placed in the bottom of the dialog, to use the two new properties. Allow me to explain what happens in the example. In a real world solution, it would obviously be spread out over multiple files instead and theĭata for the tree would likely come from an actual data source, instead of being generated on the fly. I'm sorry for the rather large amount of code in one place.
This.Propert圜hanged(this, new Propert圜hangedEventArgs(propName)) Public void NotifyPropert圜hanged(string propName)
Public event Propert圜hangedEventHandler Propert圜hanged Public class TreeViewItemBase : INotifyPropert圜hanged Public TreeViewSelectionExpansionSample() Public partial class TreeViewSelectionExpansionSample : Window Namespace WpfTutorialSamples.TreeView_control For this example, I've chosen theīase class method, because it allows me to very easily get the same functionality for my other objects. If this is not feasibleįor your solution, you could create an interface for it and then implement this instead, to establish a common ground. You could easily implement these two properties on all of your objects, but it's much easier to inherit them from a base object. TreeView, using a couple of styles targeting the TreeViewItem, inside of the ItemContainerStyle for the TreeView. These two properties are then hooked up to the The basic principle is to implement two extra properties on your data class: IsExpanded and IsSelected. Solution that lies somewhere in the middle, making it easy to implement and use, while still not being a complete hack. In this article I would like to show you a You can control the IsExpanded and IsSelected properties, to much more advanced MVVM-inspired implementations. Lots of solutions exists to handle this, ranging from "hacks" where you use the item generators of the TreeView to get the underlying TreeViewItem, where In praxis this means that you can't select orĮxpand/collapse a given node from code-behind. Longer have direct control over TreeView node specific functionality like selection and expansion state. You with one problem: Because each tree node is now represented by your custom class, for instance FamilyMember as we saw in the previous article, you no This works really well, but it does leave
In the previous couple of TreeView articles, we used data binding to display custom objects in a WPF TreeView. The TreeView control: TreeView - Selection/Expansion state