views:

913

answers:

2

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 - i.e. if I have 10 Expanders in the ListBox, I'd like only one to be open at a time... Soo at present I have:

<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... Cheers

Chris

+3  A: 

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}}}" ...>

HTH, Kent

Kent Boogaart
Bingo, thanks Kent - that's got it in one!
Chris
You're welcome Chris. If it answered your question, could you please mark it as the answer?
Kent Boogaart
Sorry - spent ages looking how to do it, the tick was hidden from me in FireFox...
Chris
A: 

This Accordian control maybe what you are looking for

Crippeoblade