// ----------------------------------------------------------------------- // // Copyright 2015 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls { using System; using System.Linq; using Mixins; using Perspex.Controls.Generators; using Perspex.Controls.Primitives; using Perspex.Rendering; using Perspex.VisualTree; /// /// An item in a . /// public class TreeViewItem : HeaderedItemsControl, ISelectable { /// /// Defines the property. /// public static readonly PerspexProperty IsExpandedProperty = PerspexProperty.Register("IsExpanded"); /// /// Defines the property. /// public static readonly PerspexProperty IsSelectedProperty = ListBoxItem.IsSelectedProperty.AddOwner(); private TreeView treeView; /// /// Initializes static members of the class. /// static TreeViewItem() { SelectableMixin.Attach(IsSelectedProperty); } /// /// Gets or sets a value indicating whether the item is expanded to show its children. /// public bool IsExpanded { get { return this.GetValue(IsExpandedProperty); } set { this.SetValue(IsExpandedProperty, value); } } /// /// Gets or sets the selection state of the item. /// public bool IsSelected { get { return this.GetValue(IsSelectedProperty); } set { this.SetValue(IsSelectedProperty, value); } } /// protected override ItemContainerGenerator CreateItemContainerGenerator() { if (this.treeView == null) { throw new InvalidOperationException( "Cannot get the ItemContainerGenerator for a TreeViewItem " + "before it is added to a TreeView."); } return this.treeView.ItemContainerGenerator; } /// protected override void OnAttachedToVisualTree(IRenderRoot root) { base.OnAttachedToVisualTree(root); if (this.GetVisualParent() != null) { this.treeView = this.GetVisualAncestors().OfType().FirstOrDefault(); if (this.treeView == null) { throw new InvalidOperationException("TreeViewItems must be added to a TreeView."); } } else { this.treeView = null; } } } }