WPF GridViewHeader样式问题

时间:2009-07-23 15:20:42

标签: wpf gridview

我正在尝试设置我的第一个WPF ListView / GridView,并且无法设置列标题的样式。我目前有3个问题。以下是GridView的视图:

alt text http://img195.imageshack.us/img195/3245/wpfgridview.png

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

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

  3. 如何在不将其拧紧的情况下覆盖单个列的水平对齐?

  4. 这就是代码的样子:

    <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>
    

    感谢您的任何想法!

    EDITED:

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

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

    产生这一点。它有效,但现在实际文本左侧有额外的空白区域。

    alt text http://img193.imageshack.us/img193/3783/wpfgridviewcolumnheader.png

1 个答案:

答案 0 :(得分:9)

我想出了我的问题。问题是列标题的行为类似于按钮,因此必须应用新模板。将其更改为TextBlock会删除所有这些问题。

<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">