Making a collection of WPF Expanders 'expand' exclusively, i.e. only one expanded at a time
Asked Answered
N

3

1

I have a ListBox containing a group of 'Expander' items, and what I would like to do is make the IsExpanded property for each of them exclusive. For example, if I have 10 Expanders in the ListBox, I'd like only one to be open at a time.

Here is what I have so far:

<Window>
    <Window.Resources>
        <DataTemplate x:Key="NormalTemplate">
            <Expander Margin="0" IsExpanded="True" Header="{Binding Model.Name}" Background="Green">
                <Grid>
                    <StackPanel HorizontalAlignment="Stretch">
                        <TextBlock Text="{Binding Model.Description}" TextWrapping="Wrap" HorizontalAlignment="Stretch" Margin="0"/>
                    </StackPanel>
                </Grid>
            </Expander>
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <ListBox ItemsSource="{Binding Groups}" 
                 ItemTemplate="{DynamicResource NormalTemplate}"
                 />
    </Grid>
</Window>

Is there any way to do this? I'm not tied to a ListBox or indeed Expanders, heck - I'm not tied to any of it if it needs to change.

Neptunium answered 22/5, 2009 at 9:54 Comment(3)
Does this answer your question? WPF - How to get only one expander expanded at any one timeMullin
@MikeNakis - That question is similar, but it was asked over a year and half after this question was asked. An answer was already accepted, again, before the other one was even conceived. My question also provides examples of what I'd tried, whereas the 'similar' one is just a 'where is it' question.Neptunium
You are right. I will be removing this comment later.Mullin
G
7

What determines whether an Expander is expanded? If it's selection, you could bind the IsExpanded property to the IsSelected property of the ListBoxItem:

<Expander IsExpanded="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" ...>
Glucinum answered 22/5, 2009 at 10:3 Comment(2)
You're welcome Chris. If it answered your question, could you please mark it as the answer?Glucinum
Sorry - spent ages looking how to do it, the tick was hidden from me in FireFox...Neptunium
D
1

Since Kent Boogaart's answer did not work for me, I did like this:

Note: I am using XAML Material Design and maybe it is the reason it does not work

<Style TargetType="{x:Type Expander}">
    <EventSetter Event="Loaded" Handler="Expander_Loaded" />
    <EventSetter Event="Expanded" Handler="Expander_Expanded" />
</Style>
private readonly List<Expander> allExpanders = new();

private void Expander_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
    allExpanders.Add(sender as Expander);
}

private void Expander_Expanded(object sender, System.Windows.RoutedEventArgs e)
{
    foreach (Expander exp in allExpanders.Where(exp => exp != sender))
    {
        exp.IsExpanded = false;
    }
}
Decretory answered 29/8, 2023 at 8:29 Comment(0)
T
0

This Accordian control maybe what you are looking for

Tobey answered 22/5, 2009 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.