我已经有一个列表框,其中包含'expander'中每个组项目的组项列表,并且首先会折叠。当将项目添加到特定组的列表框中时,当时应扩展该组的相应扩展器(IS扩展= true)。
下面是我到目前为止尝试的样式。我在这里错过了什么吗?
<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>
您需要将扩展的扩展属性设置为true以展开它。例如,您可以通过订阅组中项目的集合事件来执行此操作:
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>
,或者您可以将扩展器的属性属性绑定到您的某些来源属性。请参阅以下问题以获取有关此信息的更多信息。
WPF DataGrid自动扩展第一组