在ListView中隐藏GroupStyle.HeaderTemplate [英] Hide GroupStyle.HeaderTemplate in ListView
问题描述
我有一个XAML ListView,它的ItemsSource绑定到一个集合,并分组在DisplayName属性上.
I have a XAML ListView that has its ItemsSource bound to a collection and is grouped on a property DisplayName.
ListView显示按钮,并且使用GroupStyle分隔按钮,如下所示.
The ListView displays buttons and the buttons are separated using GroupStyle as shown below.
我正在使用Windows 7.
I am using Windows 7.
XAML
<ControlTemplate TargetType="{x:Type myNamespace:MyClass}">
<ListView ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type myNamespace:MyClass}}, Path=ItemsSource}">
<ListView.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisibility"/>
</ListView.Resources>
<ListView.ItemTemplate>
<DataTemplate>
<Button Command="{Binding}"
Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Converter={StaticResource BoolToVisibility}}">
<Image Height="24" Width="24" Source="{Binding Path=IconSource}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Button>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" FontSize="14" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
</ControlTemplate>
每当IsEnabled = false时,该按钮就从显示中隐藏,这是预期的,但是即使没有要显示的项目,组标题仍然显示.
The button is hidden from display whenever IsEnabled=false which is as expected but the group header still is displayed even if there are no items to display.
通过谷歌搜索,我尝试了以下操作,但无济于事:
From googling around I have tried the following but to no avail:
尝试1
将 HidesIfEmpty ="True" 添加到GroupStyle:
Add HidesIfEmpty="True" to the GroupStyle:
<ListView.GroupStyle>
<GroupStyle HidesIfEmpty="True">
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" FontSize="14" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
尝试2
添加与按钮相同的可见性属性:
Add a Visibility property similar to what the Button uses: Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Converter={StaticResource BoolToVisibility}}"
<ListView.GroupStyle>
<GroupStyle HidesIfEmpty="True">
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" FontSize="14" Visibility="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled, Converter={StaticResource BoolToVisibility}}"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
尝试3
只需尝试绑定HeaderTemplate的根元素的可见性: Visibility =" {Binding GroupHeaderVisibility}"
Just try to bind the Visibility of the root element of HeaderTemplate: Visibility="{Binding GroupHeaderVisibility}"
<ListView.GroupStyle>
<GroupStyle HidesIfEmpty="True">
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" FontSize="14" Visibility = "{Binding GroupHeaderVisibility}" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
没有按钮要显示时,如何隐藏GroupHeader.
How can I get the GroupHeader to hide when there are no Buttons to display.
推荐答案
据我所知,如果该按钮隐藏在ListViewItem上,则listViewItem仍然存在.
As far as I know, If the button is hidden on a ListViewItem, the listViewItem is still exist.
我建议您尝试过滤数据,仅绑定IsEnabled为真实数据,如下所示. 跨度>
mylistView.ItemsSource = DataItems.Where(p => p.IsEnabled != false).ToList();
最好的问候,
Bob
Bob
这篇关于在ListView中隐藏GroupStyle.HeaderTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!