UWP:尝试更改 ListView 项目的背景颜色时出现问题

UWP: Problem when trying to change the background color of a ListView item

我有一个带有列表视图的 uwp 桌面应用程序,我需要在其中更改给定值为空的项目的背景颜色。我已经完成了以下问题的答案所指示的所有内容,但颜色没有改变。欢迎任何帮助。

https://zamjad.wordpress.com/2010/01/01/applying-style-conditionally/

XAML

<Page.Resources>
    <Style x:Key="SpeechLineTimingSet" TargetType="ListViewItem">
        <Setter Property="Background" Value="Transparent" />
    </Style>

    <Style x:Key="SpeechLineTimingNotSet" TargetType="ListViewItem">
        <Setter Property="Background" Value="Red" />
    </Style>


    <local:SpeechLineListViewStyleSelector x:Key="SpeechLineListViewStyleSelectorObject"
                                         SpeechLineTimingSetStyle="{StaticResource SpeechLineTimingSet}"
                                         SpeechLineTimingNotSetStyle="{StaticResource SpeechLineTimingNotSet}"/>
</Page.Resources>



        <ListView x:Name="dtgSpeechLines" Grid.Column="0" Grid.Row="2" Margin="3,0,3,3" 
                  BorderBrush="Gray" BorderThickness="1" ScrollViewer.HorizontalScrollBarVisibility="Auto"
                  ItemContainerStyleSelector="{StaticResource SpeechLineListViewStyleSelectorObject}" 
                  DoubleTapped="dtgSpeechLines_DoubleTapped">
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.HeaderTemplate>
                <DataTemplate>
                    <Grid Padding="12" Margin="3,5,3,5" Background="{ThemeResource SystemBaseLowColor}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="40"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="100"/>
                            <ColumnDefinition Width="100"/>
                            <ColumnDefinition Width="100"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" x:Uid="CtrPgLvwColumn0" Style="{ThemeResource CaptionTextBlockStyle}"/>
                        <TextBlock Grid.Column="1" x:Uid="CtrPgLvwColumn1" Style="{ThemeResource CaptionTextBlockStyle}"/>
                        <TextBlock Grid.Column="2" x:Uid="CtrPgLvwColumn2" Style="{ThemeResource CaptionTextBlockStyle}"/>
                        <TextBlock Grid.Column="3" x:Uid="CtrPgLvwColumn3" Style="{ThemeResource CaptionTextBlockStyle}"/>
                        <TextBlock Grid.Column="4" x:Uid="CtrPgLvwColumn4" Style="{ThemeResource CaptionTextBlockStyle}"/>
                        <TextBlock Grid.Column="5" x:Uid="CtrPgLvwColumn5" Style="{ThemeResource CaptionTextBlockStyle}"/>
                    </Grid>
                </DataTemplate>
            </ListView.HeaderTemplate>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="3,5,3,5" >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="40"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="100"/>
                            <ColumnDefinition Width="100"/>
                            <ColumnDefinition Width="100"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" TextWrapping="Wrap" FontSize="12" Text="{Binding Number}" />
                        <TextBlock Grid.Column="1" TextWrapping="Wrap" FontSize="12" Text="{Binding OriginalText}"/>
                        <TextBlock Grid.Column="2" TextWrapping="Wrap" FontSize="12" Text="{Binding TextForAudio}"/>
                        <TextBlock Grid.Column="3" TextWrapping="Wrap" FontSize="12" Text="{Binding SubtitleNumber}"/>
                        <TextBlock Grid.Column="4" TextWrapping="Wrap" FontSize="12" Text="{Binding Start}"/>
                        <TextBlock Grid.Column="5" TextWrapping="Wrap" FontSize="12" Text="{Binding Length}" />
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

代码隐藏

 public class SpeechLineListViewStyleSelector : StyleSelector
{
    public Style SpeechLineTimingSetStyle { get; set; }
    public Style SpeechLineTimingNotSetStyle { get; set; }


    protected override Style SelectStyleCore(object item, DependencyObject container)
    {
        SpeechLine speechLine = item as SpeechLine;

        if (speechLine != null)
        {
            if (speechLine.Start == "")
            {
                return SpeechLineTimingNotSetStyle;
            }
            else
            {
                return SpeechLineTimingSetStyle;
            } 
        }

        return base.SelectStyleCore(item, container);

    }
}

请删除您在 ListView 中定义的 ListView.ItemContainerStyle。它会覆盖您在 StyleSelector 中定义的样式。只需将 HorizontalContentAlignment 设置放在 SpeechLineListViewStyleSelector.

的样式中

喜欢:

 <Style x:Key="SpeechLineTimingSet" TargetType="ListViewItem">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>

    <Style x:Key="SpeechLineTimingNotSet" TargetType="ListViewItem">
        <Setter Property="Background" Value="Red" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>

并且: