WPF GridViewHeader 样式问题 [英] WPF GridViewHeader styling questions

查看:14
本文介绍了WPF GridViewHeader 样式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置我的第一个 WPF ListView/GridView,但在设置列标题样式时遇到了问题.我目前有3个问题.这是 GridView 现在的视图:

I'm trying to set up my very first WPF ListView/GridView and having trouble with styling the column headers. I currently have 3 issues. Here is a view of the GridView as it is right now:

替代文字 http://img195.imageshack.us/img195/3245/wpfgridview.png

  1. 我想删除分隔列标题的白色垂直小边框.

  1. I want to remove the little white vertical borders that separate the column headers.

我想移除鼠标悬停效果.此屏幕截图将鼠标悬停在第 3 列上,将背景变为白色.

I want to remove the MouseOver effect. This screenshot has the mouse over the 3rd column which turns the background to white.

如何在不搞砸的情况下覆盖单个列上的水平对齐方式?

How can I override the horizontal alignment on a single column without screwing it up?

代码如下:

<Style x:Key="GrayHeader" TargetType="{x:Type GridViewColumnHeader}">
   <Setter Property="Background">
      <Setter.Value>
         <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
            <GradientStop Offset="0.0" Color="#373638" />
            <GradientStop Offset="1.0" Color="#57595B" />
         </LinearGradientBrush>
      </Setter.Value>
   </Setter>
   <Setter Property="Foreground" Value="White" />
   <Setter Property="Padding" Value="5, 5" />
   <Setter Property="BorderThickness" Value="0" />
   <Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>

<ListView ItemsSource="{Binding Source={StaticResource 
                                        EmployeeInfoDataSource}}" 
          Margin="0,20,0,20">
   <ListView.ItemContainerStyle>
      <Style TargetType="{x:Type ListViewItem}"  >
         <Setter Property="Height" Value="24" />
         <Setter Property="Background" Value="#7BB3DC" />
         <Setter Property="Foreground" Value="#000000" />
         <Setter Property="FontSize" Value="12" />
         <Setter Property="HorizontalContentAlignment" Value="Stretch" />
         <Setter Property="BorderThickness" Value="1" />
         <Setter Property="BorderBrush" Value="Gray" />
      </Style>
   </ListView.ItemContainerStyle>

   <ListView.View>
      <GridView AllowsColumnReorder="false" 
                ColumnHeaderToolTip="Employee Information"
                ColumnHeaderContainerStyle="{StaticResource GrayHeader}"
                >
         <GridViewColumn Width="200" Header="First Name">
            <GridViewColumn.CellTemplate>
               <DataTemplate>
                  <TextBlock Text="{Binding Path=FirstName}"/>
               </DataTemplate>
            </GridViewColumn.CellTemplate>
         </GridViewColumn>

         <GridViewColumn Width="300" Header="LastName">
            <GridViewColumn.CellTemplate>
               <DataTemplate>
                  <TextBlock Text="{Binding LastName}" 
                             HorizontalAlignment="Right"/>
               </DataTemplate>
            </GridViewColumn.CellTemplate>
         </GridViewColumn>

         <GridViewColumn DisplayMemberBinding="{Binding Path=EmployeeNumber}" 
                         Width="200" Header="Employee Number"/>
      </GridView>
   </ListView.View>
</ListView>

感谢您的任何想法!

要清楚我的第三个问题.这是代码:

To be clear about the 3rd question I had. This is the code:

<GridViewColumn Width="300">
    <GridViewColumnHeader HorizontalAlignment="Right">
       Last Name
    </GridViewColumnHeader>

这就产生了.它可以工作,但现在实际文本的左侧有额外的空白.

That produces this. It works but there is that extra white space in there to the left of the actual text now.

替代文字 http://img193.imageshack.us/img193/3783/wpfgridviewcolumnheader.png

推荐答案

我想出了我的问题.问题是列标题就像一个按钮,所以必须应用一个新模板.将其更改为 TextBlock 消除了所有这些问题.

I figured out my issues. The problem was that the column header acted like a button, so a new template had to be applied. Changing it to a TextBlock removed all of those issues.

<Style x:Key="GridHeaderCenter" TargetType="{x:Type GridViewColumnHeader}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                            <TextBlock Text="{TemplateBinding Content}" Padding="5" Width="{TemplateBinding Width}" TextAlignment="Center">
                                <TextBlock.Background>
                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                        <GradientStop Offset="0.0" Color="#373638" />
                                        <GradientStop Offset="1.0" Color="#77797B" />
                                    </LinearGradientBrush>
                                </TextBlock.Background>
                            </TextBlock>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="OverridesDefaultStyle" Value="True" />
                <Setter Property="Background" Value="Green" />
                <Setter Property="Foreground" Value="White" />
                <Setter Property="FontSize" Value="12" />
                <Setter Property="Background">
                    <Setter.Value>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                            <GradientStop Offset="0.0" Color="#373638" />
                            <GradientStop Offset="1.0" Color="#77797B" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Style>

<GridViewColumn Width="100" HeaderContainerStyle="{ StaticResource GridHeaderCenter}" Header="Transfer">

这篇关于WPF GridViewHeader 样式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆