WPFパンダのコレクションを作ること、すなわち一つだけが一度に展開し、独占的に「拡張」

StackOverflow https://stackoverflow.com/questions/897146

質問

私は「パンダ」の項目のグループを含むリストボックスを持っている、と私は何をしたいのは、排他的にそれらのそれぞれのためIsExpandedプロパティを作るです。私はListBoxで10頭のパンダを持っている場合たとえば、私は一つだけが一度に開くことにしたいと思います。

ここで私がこれまで持っているものです。

<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または実際にパンダに結び付けられていないよ - それは変更する必要がある場合、私はそれのいずれかに関連付けられていないよ。

役に立ちましたか?

解決

何がExpanderが展開されているかどうかを決定しますか?それは選択だ場合、あなたはIsExpandedIsSelectedプロパティにListBoxItemプロパティをバインドすることができます:

<Expander IsExpanded="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" ...>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top