Hm, already tried that, but evidently every node in the selected branch is checked for being expanded, found expanded, and selected in turn. You might want to try binding the TreeViewItem's IsSelected property to its IsExpanded property. Is there a way to get the expansion of a tree node to also mean that node's selection? Throw new NotSupportedException( " Cannot convert back." ) The children of root items are layed out // in a horizontal row. ItemsControl.ItemsControlFromItemContainer( item ) is TreeView Otherwise it is contained in another // TreeViewItem, in which case it is not a root. If the item is contained in a TreeView then it is // a root item. TreeViewItem item = itemsPresenter.TemplatedParent as TreeViewItem The ItemsPresenter's templated parent // should be a TreeViewItem. ItemsPresenter itemsPresenter = value as ItemsPresenter The 'value' argument should reference // an ItemsPresenter. Object value, Type targetType, object parameter, CultureInfo culture ) Returns 'Horizontal' for root TreeViewItems // and 'Vertical' for all other items. Public class ItemsPanelOrientationConverter : IValueConverter The control template has a Trigger to determine which element should be used to render the TreeViewItem. The ContentPresenter will be shown if the item does not have any children, in this case, if it represents a City element in the XML data. The Expander is there in case the TreeViewItem has child items. That cell contains an Expander and a ContentPresenter, but only one of those two elements will ever be visible at any given moment. That Grid has one row and one column (i.e. The XAML above creates a Border element which contains a Grid panel. I stripped away some unimportant settings, so that we can focus on the essential information: Let's take a closer look at how that is achieved. Items that represent a Country or Region XML element must render as collapsible groups, but City elements should not. That template has the job of explaining how a TreeViewItem instance should be rendered. The Style sets the Template property of TreeViewItem to a ControlTemplate. There are a couple aspects of the XAML seen above worth pointing out. In the demo application a TreeView is bound to XML data, which is in this simple format: How it works Data Formatīefore delving into the code which implements our custom layout, let's take a moment to review the data being displayed. A layout customization organizes the TreeViewItems – explaining how they should be positioned, how they should be rendered, if they should be shown or hidden, etc. In a TreeView those boxes are represented by TreeViewItem objects. the stuff that the user cares about and pays the most attention to). The content of those boxes is the data which the user consumes (i.e. An ItemsControl contains item containers, which can be thought of as 'boxes' that hold arbitrary content. TreeView and TreeViewItem both derive from ItemsControl. The item content is irrelevant for our purposes. What I refer to as a "layout customization" does not deal with rendering an item's content, rather, it explains how to render that which contains an item's content and how those containers are positioned relative to one another. The way a TreeViewItem's content renders is affected by data templates. Notice that the leaf items are rendered as hyperlinks, even though a layout customization is not in effect. Take another quick glance again at the non-customized TreeView screenshot above. If we did not apply a layout customization to the TreeView seen above, it would look like this: The bottom portion of the UI is a Frame element, which loads and displays the Wikipedia pages chosen in the TreeView. The innermost items provide links to Wikipedia pages containing information about the cities named in those items. The top portion of the user interface is the customized TreeView. The screenshot seen below is of a demo application, which is available for download via a link at the top of this page. Later we will see how this custom layout was implemented. Let's take look at what the customized TreeView looks like. In this article we will explore a more complex and interactive customization, which makes the TreeView present its items as a set of "nested buckets." Show me the money In that article we examined how to make a TreeView look like an Org Chart by using some XAML magic. In a previous article here on the CodeProject I demonstrated one way of redefining how a WPF TreeView can be displayed.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |