gpt4 book ai didi

.net - 带边距的分组 WPF ListView 样式问题

转载 作者:行者123 更新时间:2023-12-04 06:02:27 24 4
gpt4 key购买 nike

有一个很小的左侧边距 在使用 时的样式分组 WPF 中 ListView 的功能。

有分组问题的 ListView 示例(边距):

ListView, group by name

未分组的 ListView 示例(希望分组列表中的项目样式相同):

ListView, without grouping

问题:

如何删除边距/填充?分组列表中的(选定)项应填充与未分组列表中相同的空间。

更新:

            <ListView Margin="20,0,0,0" ItemsSource="{Binding ItemsView}" SelectedItem="{Binding SelectedItem}" IsSynchronizedWithCurrentItem="True"  SelectionMode="Single" BorderThickness="0" Background="Transparent">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate DataType="data:Item">
<DockPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Name}" FontWeight="Bold" Margin="0,5,5,5" />
<Separator />
</DockPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
<ListView.ItemTemplate>
<DataTemplate DataType="data:Item">
<TextBlock Margin="10,10,10,10" Text="{Binding Name}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

最佳答案

当在 CollectionViewSource 中使用分组(我假设您正在使用一个)时,Groups 将通过 GroupItem 进行可视化。 GroupItem 的默认样式如下所示(使用 StyleSnooper 获得):

<Style TargetType="{x:Type GroupItem}" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<StackPanel>
<ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" />
<ItemsPresenter Margin="5,0,0,0" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

如您所见,ItemsPresenter 上有一个 Margin。一个解决方案是为 GroupItem 创建自己的样式,并删除 ItemsPresenter 上的 Margin,并将 GroupStyle.ContainerStyle 设置为使用此样式。

关于.net - 带边距的分组 WPF ListView 样式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25239833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com