WPF:Groupstyle由于样式和/或已修改的ListBox而无法正常工作 [英] WPF: Groupstyle not working correctly because of Style and/or modified ListBox
问题描述
我尝试使用GroupStyle,但没有将组名显示为列表中项目的标题,而是仅显示标题而不显示项目.
I am trying to use a GroupStyle but instead of showing the groupname as a header over the items in the list it only shows the header and not the items.
在我对列表应用特殊样式之前,它工作正常.然后,我认为这种风格很虚假,因此我为此效果创建了一个UserControl.问题仍然存在.
It works fine until I applied a special style to the list. I then decided this style was pretty bogus so I created a UserControl fro this effect. The problem persists.
UserControl的目的是对所选项目产生扩展效果,通常它可以显示一些信息,然后在展开时显示更多信息.
The purpose of the UserControl is to have a expending effect on the selected item where normally it could show some info and then more info when expanded.
UserControl:
UserControl:
<UserControl x:Class="MyProject.CustomUC.ExpandingList"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<ListBox x:Name="List"
ItemsSource="{Binding Path=Collection}">
<ListBox.Template>
<ControlTemplate TargetType="ListBox">
<Border BorderBrush="Blue"
BorderThickness="1"
CornerRadius="2"
Background="White">
<ScrollViewer Focusable="False">
<StackPanel Margin="2" IsItemsHost="True" />
</ScrollViewer>
</Border>
</ControlTemplate>
</ListBox.Template>
<ListBox.GroupStyle>
<GroupStyle HidesIfEmpty="True">
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Margin="4" Name="Border" BorderBrush="Blue" BorderThickness="1" CornerRadius="5" Background="LightBlue">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<ContentPresenter Name="Head"
Margin="4"
Visibility="Visible"
ContentTemplate="{Binding ElementName=List, Path=DataContext.HeadTemplate}"/>
<Border Name="Tail" Visibility="Collapsed" Grid.Row="1" BorderThickness="0,1,0,0" BorderBrush="Blue">
<ContentPresenter Margin="4"
ContentTemplate="{Binding ElementName=List, Path=DataContext.TailTemplate}" />
</Border>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Tail" Property="Visibility" Value="Visible" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
</UserControl>
使用UserControl:
Using the UserControl:
<CustomUC:ExpandingList Collection="{Binding Path=List}">
<CustomUC:ExpandingList.HeadTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DisplayName}" />
</DataTemplate>
</CustomUC:ExpandingList.HeadTemplate>
<CustomUC:ExpandingList.TailTemplate>
<DataTemplate DataType="{x:Type ViewModel:ElementViewModel}">
<TextBlock Text="{Binding Path=SomeOtherProperties}" />
</DataTemplate>
</CustomUC:ExpandingList.TailTemplate>
</CustomUC:ExpandingList>
如果我从ExpandingList UserCotnrol更改,这将起作用:
This works if I change from the ExpandingList UserCotnrol:
<ListView ItemsSource="{Binding Path=List}" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DisplayName}" />
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupStyle>
<GroupStyle HidesIfEmpty="True">
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
有人知道怎么了吗?
推荐答案
在ListBox
模板中,替换:
<StackPanel Margin="2" IsItemsHost="True" />
使用
<ItemsPresenter Margin="2" />
ItemsPresenter
类具有用于组的额外逻辑,如果您直接使用面板来显示项目,则会丢失这些逻辑.
The ItemsPresenter
class has extra logic for groups, that you lose if you're directly using a panel to display the items.
这篇关于WPF:Groupstyle由于样式和/或已修改的ListBox而无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!