如何设置ListViewItem选中的背景色?
How to set ListViewItem selected background color?
是的,我知道这可能是重复的,但我看到的答案不起作用(或者我误解了)。我无法更改所选 ListViewItem 的背景颜色!这个例子非常简单和基础。
我有一个这样定义的列表源:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public List<ListViewItem> List
{
get
{
var list = new List<ListViewItem>();
for (int i = 0; i < 10; i++)
{
list.Add(new ListViewItem
{
Height = 40,
Content = i,
VerticalContentAlignment = VerticalAlignment.Center,
HorizontalContentAlignment = HorizontalAlignment.Center
});
}
return list;
}
}
}
}
而 XAML 只是:
<Window x:Class="WpfApp5.MainWindow"
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"
xmlns:local="clr-namespace:WpfApp5"
x:Name="MainWindowName"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type ListViewItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Black" />
</Style.Resources>
<Style.Triggers>
<Trigger Property="IsSelected" Value="true" >
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ListView ItemsSource="{Binding ElementName=MainWindowName, Path=List}" />
</Grid>
</Window>
据我了解,有人说标准样式触发器有效,而其他人说我需要设置 SystemColors.HighlightBrushKey
。无论如何,上述样式对所选背景没有任何改变。前景色更改正确了吗?
有人可以 post 正确 XAML 将所选 ListViewItem 的背景更改为黑色吗?
谢谢。
问题是您不能只设置 foreground/background 并期望它起作用,还有各种各样的其他事情正在发生,例如高亮显示等。在您的情况下,最简单的事情可能就是更换带有 TextBlock 的 ListViewItem ControlTemplate,然后您的 foreground/background 可以通过触发器设置。
或者换句话说,就是这样做:
<Window.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Grid Background="{TemplateBinding Background}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<TextBlock Foreground="{TemplateBinding Foreground}" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center">
<ContentPresenter />
</TextBlock>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
<Style.Triggers>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Foreground" Value="Black" />
<Setter Property="Background" Value="PaleGoldenrod" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Yellow" />
<Setter Property="Background" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
是的,我知道这可能是重复的,但我看到的答案不起作用(或者我误解了)。我无法更改所选 ListViewItem 的背景颜色!这个例子非常简单和基础。
我有一个这样定义的列表源:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace WpfApp5
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public List<ListViewItem> List
{
get
{
var list = new List<ListViewItem>();
for (int i = 0; i < 10; i++)
{
list.Add(new ListViewItem
{
Height = 40,
Content = i,
VerticalContentAlignment = VerticalAlignment.Center,
HorizontalContentAlignment = HorizontalAlignment.Center
});
}
return list;
}
}
}
}
而 XAML 只是:
<Window x:Class="WpfApp5.MainWindow"
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"
xmlns:local="clr-namespace:WpfApp5"
x:Name="MainWindowName"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type ListViewItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Black" />
</Style.Resources>
<Style.Triggers>
<Trigger Property="IsSelected" Value="true" >
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ListView ItemsSource="{Binding ElementName=MainWindowName, Path=List}" />
</Grid>
</Window>
据我了解,有人说标准样式触发器有效,而其他人说我需要设置 SystemColors.HighlightBrushKey
。无论如何,上述样式对所选背景没有任何改变。前景色更改正确了吗?
有人可以 post 正确 XAML 将所选 ListViewItem 的背景更改为黑色吗?
谢谢。
问题是您不能只设置 foreground/background 并期望它起作用,还有各种各样的其他事情正在发生,例如高亮显示等。在您的情况下,最简单的事情可能就是更换带有 TextBlock 的 ListViewItem ControlTemplate,然后您的 foreground/background 可以通过触发器设置。
或者换句话说,就是这样做:
<Window.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Grid Background="{TemplateBinding Background}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<TextBlock Foreground="{TemplateBinding Foreground}" HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center">
<ContentPresenter />
</TextBlock>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
<Style.Triggers>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Foreground" Value="Black" />
<Setter Property="Background" Value="PaleGoldenrod" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Yellow" />
<Setter Property="Background" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>