【问题标题】:ListView ignores parts of the style of item templatesListView 忽略部分项模板的样式
【发布时间】:2014-05-14 08:52:40
【问题描述】:

问题描述

我使用常规 StackPanel(在 DataTemplate 中)堆叠了一些 TextBlock 元素。

当将它用作 ContentControl 的 ContentTemplate 时,布局按预期显示。但是,如果我将 DataTemplate 用作 ListView 的 ItemTemplate,则某些属性会被简单地忽略(TextLineBounds,还有 Margin)。

在下面的屏幕截图中,我放置了一个 ContentControl 和一个 ListView,其中一个项目彼此相邻以说明问题。为了使问题更明显,我在 TextBlocks 的左侧添加了彩色网格。

我尝试过的事情

我尝试了几种使用 Blend 的方法。切换没有帮助的 ItemContainerStyle、ItemsPanel 和 ControlTemplate。 我可以使用 ItemsControl 并手动进行选择等,但我想避免这种情况。

重现问题

以下 UserControl 可用于在空白 winrt 项目中重现该问题。

<UserControl
    x:Class="ListViewRepro.DropInRepro"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="60"
    d:DesignWidth="300">
    <UserControl.Resources>
        <DataTemplate x:Key="ItemTemplate">
            <Grid Width="150">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="200" />
                </Grid.ColumnDefinitions>
                <StackPanel>
                    <Grid Height="18" Background="Yellow" />
                    <Grid Height="14" Background="Orange" />
                    <Grid Height="10" Background="GreenYellow" />
                </StackPanel>
                <StackPanel Grid.Column="1">
                    <TextBlock FontSize="20" Text="Header"
                               TextLineBounds="TrimToBaseline" />
                    <TextBlock FontSize="14.6" Text="Subheader"
                               TextLineBounds="TrimToBaseline" />
                    <TextBlock>Title</TextBlock>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </UserControl.Resources>
    <StackPanel Orientation="Horizontal">
        <ContentControl ContentTemplate="{StaticResource ItemTemplate}" />
        <ListView ItemTemplate="{StaticResource ItemTemplate}">
            <x:String>dummy item</x:String>
        </ListView>
    </StackPanel>
</UserControl>

【问题讨论】:

  • 为什么你的ListView里面有一个Grid
  • 否则,我可以看到。这可能是一个错误。我去问问。
  • 顺便说一句,它在ItemsControl 中渲染得很好。
  • 我注意到 BaselineOffset 在这两种情况下具有不同的值...
  • @FilipSkakun 网格是我能想到的最简单的项目,它会导致 ListView 显示一个项目。现在我会使用 test

标签: listview winrt-xaml


【解决方案1】:

ListViewItem 中的默认 FontFamily 不同(它是“Segoe UI”而不是“全局用户界面”),因此如果您希望它在两种情况下都一样 - 您需要指定字体明确地作为“Segoe UI”或“全局用户界面”,它的工作原理是一样的。

似乎BaselineOffset 是这两种字体之间的区别之一。

【讨论】:

  • 我需要(重新)将默认字体大小设置为 10.6666。这与设置 FontFamily 一起解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-21
  • 2011-04-05
  • 1970-01-01
  • 2014-10-28
  • 2019-11-24
  • 1970-01-01
相关资源
最近更新 更多