【问题标题】:How to change ListViewItem placeholder color in UWP?如何在 UWP 中更改 ListViewItem 占位符颜色?
【发布时间】:2018-08-02 20:48:44
【问题描述】:

对于我的 UWP 应用程序,我使用带有 ListViewRandom access data virtualization。我的问题是,对于这个特定 ListView 的内容,占位符需要是白色的。在备注下的 documentation 中似乎有资源键 ListViewItemPlaceholderBackground,但我不知道如何覆盖它。

我已尝试为我的 UserControl 实现样式资源:

我的用户控件

<UserControl
    x:Class="SimplePdfViewer.SimplePdfViewerControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SimplePdfViewer"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Unloaded="root_Unloaded"
    x:Name="root">

    <Grid>
        <!--ScrollViewer.VerticalScrollBarVisibility="Hidden"-->
        <!--ScrollViewer.ZoomMode="Disabled"-->
        <ListView x:Name="PdfListView" ItemsSource="{x:Bind DocumentDataSource}" ScrollViewer.ZoomMode="Enabled" ScrollViewer.IsScrollInertiaEnabled="True">

            <ListView.ItemTemplate>
                <DataTemplate x:DataType="BitmapImage">
                    <ListViewItem Height="1200">
                        <Image Source="{x:Bind}"/>
                    </ListViewItem>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</UserControl>

添加样式资源

<UserControl
    x:Class="SimplePdfViewer.SimplePdfViewerControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SimplePdfViewer"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Unloaded="root_Unloaded"
    x:Name="root">
    <UserControl.Resources>
        <Style TargetType="ListViewItem" x:Name="ListViewItemEdit">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListViewItem">
                        <ListViewItemPresenter ContentTransitions="{TemplateBinding ContentTransitions}"
                        PlaceholderBackground="White"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid>
        <!--ScrollViewer.VerticalScrollBarVisibility="Hidden"-->
        <!--ScrollViewer.ZoomMode="Disabled"-->
        <ListView x:Name="PdfListView" ItemsSource="{x:Bind DocumentDataSource}" ScrollViewer.ZoomMode="Enabled" ScrollViewer.IsScrollInertiaEnabled="True">

            <ListView.ItemTemplate>
                <DataTemplate x:DataType="BitmapImage">
                    <ListViewItem Height="1200" Style="{StaticResource ListViewItemEdit}">
                        <Image Source="{x:Bind}"/>
                    </ListViewItem>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</UserControl>

我没有在网上找到任何有用的东西;希望有人可以帮助我。

干杯。

【问题讨论】:

    标签: listview uwp placeholder listviewitem data-virtualization


    【解决方案1】:

    在样式中使用x:Key 代替x:Name,然后从ListViewItemContainerTemplate 属性中引用它:

    <ListView ... ItemContainerTemplate="{StaticResource ListViewItemEdit}">
    

    但是,如果你这样做,ItemContainerTemplate 中将只有部分功能,这不是你想要的。我会从docs here 复制并粘贴完整的Style,然后在那里编辑颜色。或者,您可以只提供自定义版本的画笔,而根本不编辑容器。只需删除样式并添加它即可:

    <UserControl.Resources>
        <SolidColorBrush Color="Blue" x:Key="ListViewItemPlaceholderBackgroundThemeBrush" />
    </UserControl.Resources>
    

    这应该覆盖该控件的系统默认颜色。

    【讨论】:

    • 我设法在 ListView 中使用 ItemContainerStyle 将占位符着色为白色;如果我使用 我必须对 ListViewItem 做任何事情还是将其添加到资源部分
    • 添加就够了,资源系统在搜索资源key的时候应该会捡起来
    猜你喜欢
    • 1970-01-01
    • 2019-10-09
    • 2012-08-03
    • 2012-04-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 2021-09-28
    相关资源
    最近更新 更多