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

蹲街弑〆低调 提交于 2019-12-24 12:34:07

问题


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.


回答1:


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



回答2:


This Accordian control maybe what you are looking for



来源:https://stackoverflow.com/questions/897146/making-a-collection-of-wpf-expanders-expand-exclusively-i-e-only-one-expande

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!