2
votes

I was trying to use Expander with one opened at a time. I got a code from the MSDN Forum Link.

<ListBox>
    <!-- all styles and templates-->
    <Expander Header="One"><!-- some content --></Expander>
    <Expander Header="Two"><!-- some other content --></Expander>
</ListBox>

It is working fine. Only one of the above Expander will be open at a Time.

But if I try a nested Expanders, lower Expanders doesn't open and close as desired as above code. Meaning if I add content to Expander "Two" like below

<Expander Header="Two">
    <ListBox>
        <!-- same styles and templates as above-->
        <Expander Header="Sub-One"><!-- some content --></Expander>
        <Expander Header="Sub-Two"><!-- some other content --></Expander>
    </ListBox>
</Expander>

The Expanders "Sub-One" and "Sub-Two" are some how linked to same ListBoxItem to which Expander "Two" is attached

Expanders "Sub-One" and "Sub-Two" getting expanded and collapsed at same time

I guess I need to make some change in the binding but couldn't do it.

<Style TargetType="{x:Type Expander}">
    <Setter Property="IsExpanded"
        Value="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"/>
</Style>

Any guidance will be of great help.
Note:- Few things which I tried

    - Checked parents of the Expander in the code, only ListBox is coming and there was no ListBoxItem in the hierachy
    - Tried Adding different values to AncestorLevel, no value except 1 works
    - Was thinking of Binding to ListBox SelectedIndex, this also I cannot make it work as was unable to think of Converting SelectedIndex to IsExpandable, even if I use converters
1

1 Answers

0
votes

For the style you could try Binding to the Selector.IsSelected attached property as seen here MSDN Selector.IsSelected