WPF - Only want one expander open at a time in grouped Listbox
I have a UserControl with a templated grouped listbox with expanders and only want one expander open at any time. I have browsed through the site but haven't found anything except binding the IsExpanded to IsSelected which isn't quite what I want.
I am trying to put some code in the Expanded event that would loop through Expanders and close all the ones that aren't the expander passed in the Expanded event. I can't seem to figure out how to get at them. I've tried ListBox.Items.Groups but didn't see how to get at them and tried ListBox.ItemContainerGenerator.ContainerFromItem (or Index) but nothing came back.
Thanks
Here is th开发者_JAVA技巧e current markup:
<ListBox Name="ListBox">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Border BorderBrush="CadetBlue" BorderThickness="1">
<Expander BorderThickness="0,0,0,1" Expanded="Expander_Expanded"
Focusable="False"
IsExpanded="{Binding IsSelected,
RelativeSource={RelativeSource FindAncestor, AncestorType=
{x:Type ListBoxItem}}}" >
<Expander.Header>
<Grid>
<StackPanel Height="30" Orientation="Horizontal">
<TextBlock Foreground="Navy" FontWeight="Bold"
Text="{Binding Path=Name}" Margin="5,0,0,0"
MinWidth="200" Padding="3"
VerticalAlignment="Center" />
<TextBlock Foreground="Navy" FontWeight="Bold"
Text=" Setups: " VerticalAlignment="Center"
HorizontalAlignment="Right"/>
<TextBlock Foreground="Navy" FontWeight="Bold"
Text="{Binding Path=ItemCount}"
VerticalAlignment="Center"
HorizontalAlignment="Right" />
</StackPanel>
</Grid>
</Expander.Header>
<Expander.Content>
<Grid Background="white" >
<ItemsPresenter />
</Grid>
</Expander.Content>
<Expander.Style >
<Style TargetType="{x:Type Expander}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0"
EndPoint="0,1">
<GradientStop Color="WhiteSmoke"
Offset="0.0" />
<GradientStop Color="Orange" Offset="1.0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="false"
<Setter Property="Background">
<Setter.Value>
...
In the ListBoxItem's template you can use RadioButtons that share the same group, bind their IsChecked
to IsSelected
of the ListBoxItem
and retemplate it as Expander, so you can bind the IsExpanded
to IsChecked
on the TemplatedParent
.
精彩评论