2016-06-11 62 views
2

我一直在試圖學習如何自定義一個包含GridView的ListView。WPF如何更改gridview的頁眉顏色

我已經能夠找出並學習我需要的每個部分...除了一個。

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 = "[email protected]" }); 
      items.Add(new User() { Name = "Jane Doe", Age = 39, Mail = "[email protected]" }); 
      items.Add(new User() { Name = "Sammy Doe", Age = 7, Mail = "[email protected]" }); 
      lvUsers.ItemsSource = items; 
     } 

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

      public int Age { get; set; } 

      public string Mail { get; set; } 

     } 
    } 
} 

回答

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> 

看看這個幫助。

+0

工作......它也解決了另一件事 - 當我把鼠標放在標題上時刪除高亮顯示。 – vicky96