0
votes

I've a list box with list of group items enclosed each group item in 'Expander' and it's collapsed always at first instance. When an item is added to the list box to a particular group then at that time the respective expander for that group should be expanded(IsExpanded=true).

Below is the style I've tried so far. Am i missing anything here?

<Style x:Key="GroupContainerStyleA" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Expander IsExpanded="False" Style="{StaticResource ExpanderStyle}">
                            <Expander.Header>
                                <Border>
                                    <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="White"/>
                                </Border>
                            </Expander.Header>
                            <Border Background="White" Margin="1.5,0,1.5,1.5" BorderBrush="{StaticResource SideButtonBackgroundBrushKey}" BorderThickness="0.5">
                                <ItemsPresenter Margin="5,0,0,5"/>
                            </Border>
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
1

1 Answers

1
votes

You need to set the IsExpanded property of the Expander to true to expand it. You could for example do this by subscribing to the CollectionChanged event of the items in the group:

private void Expander_Loaded(object sender, RoutedEventArgs e)
{
    Expander expander = sender as Expander;
    CollectionViewGroup cvs = expander.DataContext as CollectionViewGroup;
    if (cvs != null)
    {
        INotifyCollectionChanged coll = cvs.Items as INotifyCollectionChanged;
        if (coll != null)
        {
            WeakEventManager<INotifyCollectionChanged, NotifyCollectionChangedEventArgs>.AddHandler(coll, "CollectionChanged",
                (ss, ee) => expander.IsExpanded = true);
        }
    }

}

<Style TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Expander IsExpanded="False" Loaded="Expander_Loaded">
                    <Expander.Header>
                        <Border>
                            <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Foreground="White"/>
                        </Border>
                    </Expander.Header>
                    <Border Background="White" Margin="1.5,0,1.5,1.5" BorderBrush="Black" BorderThickness="0.5">
                        <ItemsPresenter Margin="5,0,0,5"/>
                    </Border>
                </Expander>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Or you could bind to the IsExpanded property of the Expander to some source property of yours. Please refer to the following question for more information about this.

wpf datagrid automatically expand first group