如何删除 margins/padding 并为 UWP 中的分组 ListView 启用全宽?
How do I remove the margins/padding and enable full width for a grouped ListView in UWP?
我有一个 ListView
与 header 分组。我有一个用于 header <local:MyHeaderUserControl />
的用户控件,它使用文本块 <TextBlock Text="{Binding Key}" />
显示 header,另一个用户控件用于每个条目 <local:MyListItemUserControl>
,它绑定到列出的 object.
我想全宽显示它们,没有边距。 ListView
在 UWP 中插入恼人的边距、分隔线,并且默认情况下左对齐,并且不清楚我必须在许多可能的模板中设置哪些属性将删除这些属性。
执行此操作的最小模板是什么?
注意:我已经解决了这个问题,希望 post 可以参考,但我很乐意让其他人先得到业力 ;)
这是我能找到的最小的模板,但它也删除了焦点、选择、动画等的默认样式,这些都必须在自定义 UserControls 中处理...
<ListView
ItemsSource="{Binding Source={StaticResource collectionViewSource}}"
>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<local:MyListItemUserControl />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderContainerStyle>
<Style TargetType="ListViewHeaderItem">
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewHeaderItem">
<local:MyHeaderUserControl />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.HeaderContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
或者,此模板保留单击、选择等的默认行为,同时删除边距并使控件全宽...
<ListView
ItemsSource="{Binding Source={StaticResource collectionViewSource}}"
>
<ListView.ItemTemplate>
<DataTemplate>
<!-- This is marginless and full width! -->
<local:MyListItemUserControl />
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
<Setter Property="Background" Value="{ThemeResource ListViewItemBackground}"/>
<Setter Property="Foreground" Value="{ThemeResource ListViewItemForeground}"/>
<Setter Property="TabNavigation" Value="Local"/>
<Setter Property="IsHoldingEnabled" Value="True"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}"/>
<Setter Property="MinHeight" Value="0"/>
<Setter Property="AllowDrop" Value="False"/>
<Setter Property="UseSystemFocusVisuals" Value="True"/>
<Setter Property="FocusVisualMargin" Value="0"/>
<Setter Property="FocusVisualPrimaryBrush" Value="{ThemeResource ListViewItemFocusVisualPrimaryBrush}"/>
<Setter Property="FocusVisualPrimaryThickness" Value="2"/>
<Setter Property="FocusVisualSecondaryBrush" Value="{ThemeResource ListViewItemFocusVisualSecondaryBrush}"/>
<Setter Property="FocusVisualSecondaryThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter CheckBrush="{ThemeResource ListViewItemCheckBrush}" ContentMargin="{TemplateBinding Padding}" CheckMode="{ThemeResource ListViewItemCheckMode}" ContentTransitions="{TemplateBinding ContentTransitions}" CheckBoxBrush="{ThemeResource ListViewItemCheckBoxBrush}" DragForeground="{ThemeResource ListViewItemDragForeground}" DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}" DragBackground="{ThemeResource ListViewItemDragBackground}" DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}" FocusVisualPrimaryBrush="{TemplateBinding FocusVisualPrimaryBrush}" FocusVisualSecondaryThickness="{TemplateBinding FocusVisualSecondaryThickness}" FocusBorderBrush="{ThemeResource ListViewItemFocusBorderBrush}" FocusVisualMargin="{TemplateBinding FocusVisualMargin}" FocusVisualPrimaryThickness="{TemplateBinding FocusVisualPrimaryThickness}" FocusSecondaryBorderBrush="{ThemeResource ListViewItemFocusSecondaryBorderBrush}" FocusVisualSecondaryBrush="{TemplateBinding FocusVisualSecondaryBrush}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Control.IsTemplateFocusTarget="True" PointerOverForeground="{ThemeResource ListViewItemForegroundPointerOver}" PressedBackground="{ThemeResource ListViewItemBackgroundPressed}" PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackground}" PointerOverBackground="{ThemeResource ListViewItemBackgroundPointerOver}" ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}" SelectedPressedBackground="{ThemeResource ListViewItemBackgroundSelectedPressed}" SelectionCheckMarkVisualEnabled="{ThemeResource ListViewItemSelectionCheckMarkVisualEnabled}" SelectedForeground="{ThemeResource ListViewItemForegroundSelected}" SelectedPointerOverBackground="{ThemeResource ListViewItemBackgroundSelectedPointerOver}" SelectedBackground="{ThemeResource ListViewItemBackgroundSelected}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<!-- This is marginless and full width! -->
<local:MyHeaderUserControl />
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.HeaderContainerStyle>
<Style TargetType="ListViewHeaderItem">
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
<Setter Property="FontSize" Value="{ThemeResource ListViewHeaderItemThemeFontSize}"/>
<Setter Property="Background" Value="{ThemeResource ListViewHeaderItemBackground}"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="MinHeight" Value="0"/>
<Setter Property="UseSystemFocusVisuals" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewHeaderItem">
<StackPanel BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.HeaderContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
发生这种情况的原因是,每次您添加不是 ListViewitem 的内容时,它都会自动包裹在 ListViewItem 周围,并在其左右应用大约 10 个边距。
做一个聪明的玩家,玩得轻松而肮脏。
您无需为 100 行样式 xalm 行而烦恼,您只需在自定义项目的左右两侧应用负 -10 边距即可。这将强制列表项覆盖列表视图自动应用的 10 像素边距。
这当然意味着你首先要创建一个listviewitem,然后将这个listviewitem的内容设置为你的自定义项,最后将listviewitem添加到你的ListView中。
MyListItemUserControl item = new MyListItemUserControl();
item.Height = 45;
item.Margin = new Thickness(-10, 4, -10, 4);
item.HorizontalAlignment = HorizontalAlignment.Stretch;
ListViewItem ListItem = new ListViewItem();
ListItem.HorizontalAlignment =
HorizontalAlignment.Stretch;
ListItem.HorizontalContentAlignment = HorizontalAlignment.Stretch;
ListItem.Content =item;
LIST.Items.Add(ListItem);
这里有一个更简单的方法:为您的 ListView
.
声明一个 Style
ListView 的示例 XAML:
<ListView
ItemContainerStyle="{StaticResource ListViewItemStretchStyle}"
...
</ListView>
样式如下:
<Style x:Key="ListViewItemStretchStyle" TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
</Style>
我还包括了 Stretch
,因为这通常是 ListView 用法所需要的。
您可以在包含 ListView
的页面中包含 Style
。请务必在 ListView 声明之前声明它。
我有一个 ListView
与 header 分组。我有一个用于 header <local:MyHeaderUserControl />
的用户控件,它使用文本块 <TextBlock Text="{Binding Key}" />
显示 header,另一个用户控件用于每个条目 <local:MyListItemUserControl>
,它绑定到列出的 object.
我想全宽显示它们,没有边距。 ListView
在 UWP 中插入恼人的边距、分隔线,并且默认情况下左对齐,并且不清楚我必须在许多可能的模板中设置哪些属性将删除这些属性。
执行此操作的最小模板是什么?
注意:我已经解决了这个问题,希望 post 可以参考,但我很乐意让其他人先得到业力 ;)
这是我能找到的最小的模板,但它也删除了焦点、选择、动画等的默认样式,这些都必须在自定义 UserControls 中处理...
<ListView
ItemsSource="{Binding Source={StaticResource collectionViewSource}}"
>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<local:MyListItemUserControl />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderContainerStyle>
<Style TargetType="ListViewHeaderItem">
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewHeaderItem">
<local:MyHeaderUserControl />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.HeaderContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
或者,此模板保留单击、选择等的默认行为,同时删除边距并使控件全宽...
<ListView
ItemsSource="{Binding Source={StaticResource collectionViewSource}}"
>
<ListView.ItemTemplate>
<DataTemplate>
<!-- This is marginless and full width! -->
<local:MyListItemUserControl />
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
<Setter Property="Background" Value="{ThemeResource ListViewItemBackground}"/>
<Setter Property="Foreground" Value="{ThemeResource ListViewItemForeground}"/>
<Setter Property="TabNavigation" Value="Local"/>
<Setter Property="IsHoldingEnabled" Value="True"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}"/>
<Setter Property="MinHeight" Value="0"/>
<Setter Property="AllowDrop" Value="False"/>
<Setter Property="UseSystemFocusVisuals" Value="True"/>
<Setter Property="FocusVisualMargin" Value="0"/>
<Setter Property="FocusVisualPrimaryBrush" Value="{ThemeResource ListViewItemFocusVisualPrimaryBrush}"/>
<Setter Property="FocusVisualPrimaryThickness" Value="2"/>
<Setter Property="FocusVisualSecondaryBrush" Value="{ThemeResource ListViewItemFocusVisualSecondaryBrush}"/>
<Setter Property="FocusVisualSecondaryThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter CheckBrush="{ThemeResource ListViewItemCheckBrush}" ContentMargin="{TemplateBinding Padding}" CheckMode="{ThemeResource ListViewItemCheckMode}" ContentTransitions="{TemplateBinding ContentTransitions}" CheckBoxBrush="{ThemeResource ListViewItemCheckBoxBrush}" DragForeground="{ThemeResource ListViewItemDragForeground}" DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}" DragBackground="{ThemeResource ListViewItemDragBackground}" DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}" FocusVisualPrimaryBrush="{TemplateBinding FocusVisualPrimaryBrush}" FocusVisualSecondaryThickness="{TemplateBinding FocusVisualSecondaryThickness}" FocusBorderBrush="{ThemeResource ListViewItemFocusBorderBrush}" FocusVisualMargin="{TemplateBinding FocusVisualMargin}" FocusVisualPrimaryThickness="{TemplateBinding FocusVisualPrimaryThickness}" FocusSecondaryBorderBrush="{ThemeResource ListViewItemFocusSecondaryBorderBrush}" FocusVisualSecondaryBrush="{TemplateBinding FocusVisualSecondaryBrush}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Control.IsTemplateFocusTarget="True" PointerOverForeground="{ThemeResource ListViewItemForegroundPointerOver}" PressedBackground="{ThemeResource ListViewItemBackgroundPressed}" PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackground}" PointerOverBackground="{ThemeResource ListViewItemBackgroundPointerOver}" ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}" SelectedPressedBackground="{ThemeResource ListViewItemBackgroundSelectedPressed}" SelectionCheckMarkVisualEnabled="{ThemeResource ListViewItemSelectionCheckMarkVisualEnabled}" SelectedForeground="{ThemeResource ListViewItemForegroundSelected}" SelectedPointerOverBackground="{ThemeResource ListViewItemBackgroundSelectedPointerOver}" SelectedBackground="{ThemeResource ListViewItemBackgroundSelected}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<!-- This is marginless and full width! -->
<local:MyHeaderUserControl />
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.HeaderContainerStyle>
<Style TargetType="ListViewHeaderItem">
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
<Setter Property="FontSize" Value="{ThemeResource ListViewHeaderItemThemeFontSize}"/>
<Setter Property="Background" Value="{ThemeResource ListViewHeaderItemBackground}"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="MinHeight" Value="0"/>
<Setter Property="UseSystemFocusVisuals" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewHeaderItem">
<StackPanel BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
<ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.HeaderContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>
发生这种情况的原因是,每次您添加不是 ListViewitem 的内容时,它都会自动包裹在 ListViewItem 周围,并在其左右应用大约 10 个边距。
做一个聪明的玩家,玩得轻松而肮脏。
您无需为 100 行样式 xalm 行而烦恼,您只需在自定义项目的左右两侧应用负 -10 边距即可。这将强制列表项覆盖列表视图自动应用的 10 像素边距。
这当然意味着你首先要创建一个listviewitem,然后将这个listviewitem的内容设置为你的自定义项,最后将listviewitem添加到你的ListView中。
MyListItemUserControl item = new MyListItemUserControl();
item.Height = 45;
item.Margin = new Thickness(-10, 4, -10, 4);
item.HorizontalAlignment = HorizontalAlignment.Stretch;
ListViewItem ListItem = new ListViewItem();
ListItem.HorizontalAlignment =
HorizontalAlignment.Stretch;
ListItem.HorizontalContentAlignment = HorizontalAlignment.Stretch;
ListItem.Content =item;
LIST.Items.Add(ListItem);
这里有一个更简单的方法:为您的 ListView
.
Style
ListView 的示例 XAML:
<ListView
ItemContainerStyle="{StaticResource ListViewItemStretchStyle}"
...
</ListView>
样式如下:
<Style x:Key="ListViewItemStretchStyle" TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
</Style>
我还包括了 Stretch
,因为这通常是 ListView 用法所需要的。
您可以在包含 ListView
的页面中包含 Style
。请务必在 ListView 声明之前声明它。