1

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 the 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>
... 
3
  • Why isn't 'it quite what you want' ?
    – Aviad P.
    May 28, 2010 at 21:10
  • 1
    If I understand the other answer, a user would have to select an item in the list box to close any open the expanders. In my case a user maybe looking for an item and not have actually selected anything yet and is opening expanders to do a visual search. We only want one expander/group opened at any time to save on screen real estate. I did try binding IsExpanded to IsSelected, to see what would happen, but nothing happened. <Expander IsExpanded="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}">
    – Portsmouth
    Jun 1, 2010 at 13:05
  • Got it figured out. Found a blog on the MSDN site. Did some minor modification to the code sugggestion and put it into the Expanded event on my control. The trick is to get to the ItemsPresenter and then drill down through the children to the expander of each item in the ItemsPresenter. In my case the children were the ItemPresenter StackPanel, Border and the Expander social.msdn.microsoft.com/forums/en-us/wpf/thread/…
    – Portsmouth
    Jun 2, 2010 at 17:43

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.