如何删除边距/填充并为UWP中的分组ListView启用全宽?

时间:2017-10-07 20:17:35

标签: xaml listview uwp uwp-xaml templating

我有一个ListView与标题分组。我有一个用于标题<local:MyHeaderUserControl />的UserControl,它使用文本块<TextBlock Text="{Binding Key}" />显示标题,并为绑定到列出的对象的每个条目<local:MyListItemUserControl>显示另一个UserControl。

我想要显示全宽而没有边距。 UWP中的ListView插入恼人的边距,分隔线,默认情况下左对齐,不清楚我必须设置哪些属性可以删除那些可能的模板。

这样做的最小模板是什么?

注意:我已经完成了这项工作,我希望能够在我能够的时候发帖以供参考,但如果他们先到达那里,我很乐意让其他人获得业力;)

2 个答案:

答案 0 :(得分:0)

这是我能找到的最小模板,但它也删除了焦点,选择,动画等的默认样式,这些样式都必须在自定义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>

答案 1 :(得分:0)

发生这种情况的原因是,每次添加不是ListViewitem的东西时,它会自动缠绕在ListViewItem上,并且左右应用粗略的10边距。

聪明的玩家可以轻松而肮脏。

您可以在自定义项目的左右两侧添加负-10边距,而不是使用100行造型xalm线条。这将强制列表项覆盖listview自动应用的10 px边距。

这当然意味着您将首先创建一个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);