专门对WPF扩展器的集合进行“扩展",即一次仅扩展一个 [英] Making a collection of WPF Expanders 'expand' exclusively, i.e. only one expanded at a time

查看:94
本文介绍了专门对WPF扩展器的集合进行“扩展",即一次仅扩展一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一组"Expander"项的ListBox,我想做的是使每个项的IsExpanded属性都互斥.例如,如果ListBox中有10个扩展器,则一次只能打开一个.

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>

有没有办法做到这一点?我没有绑定到ListBox或扩展器,哎呀-如果需要更改,我也没有绑定到任何扩展器.

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.

推荐答案

由什么决定Expander是否展开?如果选择,则可以将IsExpanded属性绑定到ListBoxItemIsSelected属性:

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

这篇关于专门对WPF扩展器的集合进行“扩展",即一次仅扩展一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆