Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The accepted answer is wrong as explained in the comments. I wrote the following behavior which achieves the desired functionality:</p> <pre><code>public class PersistGroupExpandedStateBehavior : Behavior&lt;Expander&gt; { #region Static Fields public static readonly DependencyProperty GroupNameProperty = DependencyProperty.Register( "GroupName", typeof(object), typeof(PersistGroupExpandedStateBehavior), new PropertyMetadata(default(object))); private static readonly DependencyProperty ExpandedStateStoreProperty = DependencyProperty.RegisterAttached( "ExpandedStateStore", typeof(IDictionary&lt;object, bool&gt;), typeof(PersistGroupExpandedStateBehavior), new PropertyMetadata(default(IDictionary&lt;object, bool&gt;))); #endregion #region Public Properties public object GroupName { get { return (object)this.GetValue(GroupNameProperty); } set { this.SetValue(GroupNameProperty, value); } } #endregion #region Methods protected override void OnAttached() { base.OnAttached(); bool? expanded = this.GetExpandedState(); if (expanded != null) { this.AssociatedObject.IsExpanded = expanded.Value; } this.AssociatedObject.Expanded += this.OnExpanded; this.AssociatedObject.Collapsed += this.OnCollapsed; } protected override void OnDetaching() { this.AssociatedObject.Expanded -= this.OnExpanded; this.AssociatedObject.Collapsed -= this.OnCollapsed; base.OnDetaching(); } private ItemsControl FindItemsControl() { DependencyObject current = this.AssociatedObject; while (current != null &amp;&amp; !(current is ItemsControl)) { current = VisualTreeHelper.GetParent(current); } if (current == null) { return null; } return current as ItemsControl; } private bool? GetExpandedState() { var dict = this.GetExpandedStateStore(); if (!dict.ContainsKey(this.GroupName)) { return null; } return dict[this.GroupName]; } private IDictionary&lt;object, bool&gt; GetExpandedStateStore() { ItemsControl itemsControl = this.FindItemsControl(); if (itemsControl == null) { throw new Exception( "Behavior needs to be attached to an Expander that is contained inside an ItemsControl"); } var dict = (IDictionary&lt;object, bool&gt;)itemsControl.GetValue(ExpandedStateStoreProperty); if (dict == null) { dict = new Dictionary&lt;object, bool&gt;(); itemsControl.SetValue(ExpandedStateStoreProperty, dict); } return dict; } private void OnCollapsed(object sender, RoutedEventArgs e) { this.SetExpanded(false); } private void OnExpanded(object sender, RoutedEventArgs e) { this.SetExpanded(true); } private void SetExpanded(bool expanded) { var dict = this.GetExpandedStateStore(); dict[this.GroupName] = expanded; } #endregion } </code></pre> <p>It attaches a dictionary to the containing <code>ItemsControl</code> which saves the expanded state for every group item. This will be peristent, even if the items in the control changes.</p> <p><strong>Usage:</strong></p> <pre><code>&lt;Expander&gt; &lt;i:Interaction.Behaviors&gt; &lt;behaviors:PersistGroupExpandedStateBehavior GroupName="{Binding Name}" /&gt; &lt;/i:Interaction.Behaviors&gt; ... &lt;/Expander&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload