WPF中的ListView选择颜色

时间:2018-08-10 05:27:07

标签: c# wpf xaml listview

我是WPF的新手,我不了解如何在“列表”视图中更改选择颜色。我已经尝试了很多事情,但是我无法改变。以下是代码:

MainWindow.xaml:

<Window x:Class="MyListView.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:MyListView"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Window.Resources>
        <Style TargetType="ListViewItem">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
            </Style.Resources>
        </Style>
    </Window.Resources>
    <Grid>
        <ListView x:Name="MovieListView" Margin="30,0,50,48" Height="344" VerticalAlignment="Bottom">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Path= MovieID}" />
                        <TextBlock Text="{Binding Path= MovieName}" FontWeight="Bold" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

    </Grid>
</Window> 

MainWindow.xaml.cs

namespace MyListView
{
    public class MyDataModel
    {
        public String MovieName { get; set; }
        public String MovieID { get; set; }

        public MyDataModel(String ID, String Name)
        {
            MovieID = ID;
            MovieName = Name;
        }
    }
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            MovieListView.Items.Add(new MyDataModel("MV_1","Movie 1" ));
            MovieListView.Items.Add(new MyDataModel("MV_2", "Movie 2"));
            MovieListView.Items.Add(new MyDataModel("MV_3", "Movie 3"));
        }
    }
}

能否请您向我解释如何更改选择颜色。

编辑1:添加了ScreenShot

enter image description here

2 个答案:

答案 0 :(得分:2)

如果您使用的是Windows 8或更高版本,则应为.ckpt容器定义自定义ControlTemplate

ListViewItem

enter image description here

答案 1 :(得分:0)

看到对我有用的东西!

您使用的是哪个Windows版本?