【问题标题】:Grouped WPF ListView Styling issue with margin带边距的分组 WPF ListView 样式问题
【发布时间】:2014-08-11 09:21:01
【问题描述】:

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

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

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

问题:

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

更新:

            <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>

【问题讨论】:

  • 尝试为ItemsControl.GroupStyle Property 定义一个Style
  • 已经做了...边距(设置边距=“0”),但仍然存在用于分组项目。究竟需要设置什么?
  • 可能是Padding...找出答案的最佳方法是对各种内部控制的Background 进行不同的着色。这样,您将能够看到哪个控件占用了空间并采取相应措施。
  • @Sheridan Padding 有什么控件?这没有帮助,我不知道要设计哪个对象。它不是 DataItemTemplate 的一部分,也不是 GroupStyle.HeaderTemplate 的一部分。
  • 然后显示演示此问题的相关 XAML。

标签: .net wpf listview


【解决方案1】:

当在 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 设置为使用此样式。

【讨论】:

  • 不知道为什么MS默认使用5,0,0,0。
猜你喜欢
  • 2012-03-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
  • 2013-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多