Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks Jobi. That's pretty clever. The rabbit hole of WPF keeps getting deeper and deeper.</p> <p>Here is what I did based on your suggestion:</p> <pre><code>private void Expander_Expanded(object sender, RoutedEventArgs e) { DependencyObject dep = (DependencyObject)sender; while ((dep != null) &amp;&amp; !(dep is ListBoxItem)) { dep = VisualTreeHelper.GetParent(dep); } if (dep == null) return; int index = PersonList.ItemContainerGenerator.IndexFromContainer(dep); PersonList.SelectedIndex = index; } private void Expander_Collapsed(object sender, RoutedEventArgs e) { DependencyObject dep = (DependencyObject)sender; while ((dep != null) &amp;&amp; !(dep is ListBoxItem)) { dep = VisualTreeHelper.GetParent(dep); } if (dep == null) return; int index = PersonList.ItemContainerGenerator.IndexFromContainer(dep); if (PersonList.SelectedIndex == index) PersonList.SelectedIndex = -1; } </code></pre> <p>I had to change the ListViewItem to ListBoxItem (I was using a ListBox).</p> <p>Also, I used the index to select or de-select the ListBox.SelectedIndex. This give me the experience I was looking for. </p> <ol> <li><p>The first time someone expands an Expander, it selects the newly expanded ListBoxItem.</p></li> <li><p>If someone expands another Expander, the previous ListBoxItem is deselected, but remains expanded, the newly expanded ListBoxItem is selected.</p></li> <li><p>If someone collapses a selected Expander, the ListBoxItem is deselected.</p></li> <li><p>If there are several Expanders expanded, someone collapses a non-selected ListBoxItem expander, the previously selected ListBoxItem remains selected.</p></li> </ol> <p>Thanks for the help - I think this is a very useful little code snippet for anyone who uses Expanders in a ListBox.</p>
    singulars
    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. 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