Wpf ListView控件中选定行的背景不会更改

Wpf ListView控件中选定行的背景不会更改,wpf,xaml,listview,background,Wpf,Xaml,Listview,Background,我有以下带有ListView.ItemTemplate的ListView: <ListView.ItemTemplate> <DataTemplate> <StackPanelName="stackPanel" Orientation="Horizontal"> <TextBoxName="textBoxOrg" Background="Transparent" Border

我有以下带有ListView.ItemTemplate的ListView:

<ListView.ItemTemplate>
    <DataTemplate>
        <StackPanelName="stackPanel" Orientation="Horizontal">
            <TextBoxName="textBoxOrg"
                Background="Transparent" BorderThickness="0" TextWrapping="Wrap" Text="{BindingOrgText}"
                IsReadOnly="True"/>
            <TextBoxName="textBoxNew"
                Background="Transparent" BorderThickness="0" TextWrapping="Wrap" Text="{BindingNewText}"
                AcceptsReturn="True"/>
        </StackPanel>
    </DataTemplate>
</ListView.ItemTemplate>
以及下面的ListViewItemStyle

<Style TargetType="ListViewItem">
    <Setter Property="SnapsToDevicePixels" Value="True" />
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="LightGoldenrodYellow" />
            </Trigger>
        </Style.Triggers>
</Style>
我想更改所选项目的默认“蓝色”背景色,但在使用上述代码时,在我选择项目时,它没有更改为“LightGoldenrodYellow”


如何修复代码以使其正常工作?

您需要自定义ListViewItem的ControlTemplate。否则它将重写,而不使用您定义的后台触发器。下面是自定义的默认模板我正在使用一个名为stylesnooper的小工具来获取模板:


WPF博士有一篇关于ItemsControl的优秀文章,名为并阅读I和L,我最终得出如下结论:

<Style x:Key="myListboxStyle">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#CCFFFFFF" />
    </Style.Resources>
</Style>
它工作得很好。 谢谢大家

<Style x:Key="myListboxStyle">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#CCFFFFFF" />
    </Style.Resources>
</Style>