WPF如何更改gridview的标题颜色

时间:2016-06-11 04:58:20

标签: c# wpf xaml listview gridview

我一直在努力学习如何自定义包含GridView的ListView。

我已经能够找出并学习我需要的每个部分......除了一个。

enter image description here

如何更改每个标题列名称之间的白线颜色?

XAML:

<Window x:Class="UITest.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:UITest"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="400">
    <StackPanel>
        <ListView Margin="0 50 0 0" Name="lvUsers" BorderBrush="{x:Null}" BorderThickness="0" Background="Red" Padding="0">
            <ListView.View>
                <GridView>
                    <GridView.ColumnHeaderContainerStyle>
                        <Style TargetType="{x:Type GridViewColumnHeader}">
                            <Setter Property="Background" Value="DarkBlue"></Setter>
                            <Setter Property="Foreground" Value="White"></Setter>
                            <Setter Property="BorderBrush" Value="{x:Null}"></Setter>
                            <Setter Property="BorderThickness" Value="0"></Setter>
                            <Setter Property="Margin" Value="0"></Setter>
                            <Setter Property="Padding" Value="0"></Setter>
                        </Style>
                    </GridView.ColumnHeaderContainerStyle>
                    <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
                    <GridViewColumn Header="Mail" Width="150" DisplayMemberBinding="{Binding Mail}" />
                </GridView>
            </ListView.View>
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="Background" Value="Blue"></Setter>
                    <Setter Property="Foreground" Value="White"></Setter>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>
    </StackPanel>
</Window>

这是.CS代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UITest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<User> items = new List<User>();
            items.Add(new User() { Name = "John Doe", Age = 42, Mail = "john@doe-family.com" });
            items.Add(new User() { Name = "Jane Doe", Age = 39, Mail = "jane@doe-family.com" });
            items.Add(new User() { Name = "Sammy Doe", Age = 7, Mail = "sammy.doe@gmail.com" });
            lvUsers.ItemsSource = items;
        }

        public class User
        {
            public string Name { get; set; }

            public int Age { get; set; }

            public string Mail { get; set; }

        }
    }
}

1 个答案:

答案 0 :(得分:1)

这就是我在申请中的表现......

<Style TargetType="{x:Type GridViewColumnHeader}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                <Border BorderThickness="0,0,0,1" BorderBrush="White" Background="Transparent">
                    <TextBlock x:Name="ContentHeader" Text="{TemplateBinding Content}" Padding="5,5,5,0" Width="{TemplateBinding Width}" TextAlignment="Center" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="Foreground" Value="White" />
</Style>

看看这是否有帮助。