3

我现在有以下屏幕,我想删除标题后面的双色调背景,红色箭头指向它所在的位置,绿色突出显示它覆盖的区域。

在此处输入图像描述

在正常情况下,您不会看到它,但是因为我在第 5 列中有文本框,会拉伸它出现的列标题。

我想删除它,使其透明,但我不知道 DataGrid 上的组件或如何访问它。有人能帮我吗?

以下网页有一个精彩的组件图http://blog.smoura.com/wpf-toolkit-datagrid-part-ii-custom-styling/它显示了定义为 DataGridColumnHeadersPresenter 的相同类型的区域,我不是确定这是否是我应该瞄准的目标。我没有太多运气就试了一下。 在此处输入图像描述

这是我的完整 XAML

<UserControl x:Class="Users.View.AllUsersView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" 
             mc:Ignorable="d" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:helpers="clr-namespace:Users.ViewModel.Helpers"
             d:DesignHeight="160" d:DesignWidth="1100">

    <UserControl.Resources>
        <CollectionViewSource x:Key="UserCollection" Source="{Binding Path=AllUsers}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Name" Direction="Ascending" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

        <Style x:Key="UserItemStyle" TargetType="{x:Type DataGridRow}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="ItemsControl.AlternationIndex" Value="1" />
                        <Condition Property="IsSelected" Value="False" />
                        <Condition Property="IsMouseOver" Value="False" />
                    </MultiTrigger.Conditions>
                    <Setter Property="Background" Value="#EEEEEEEE" />
                </MultiTrigger>
            </Style.Triggers>
        </Style>

        <DataTemplate x:Key="headerTemplate">
            <TextBlock Padding="0,0,0,0" Margin="0,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Bottom" Text="{Binding}"/>
        </DataTemplate>

        <Style x:Key="RowHeaderStyle2" TargetType="DataGridRowsPresenter">
            <Setter Property="Background" Value="{x:Null}"/>
        </Style>
        <Style x:Key="ColumnHeaderStyle" TargetType="DataGridColumnHeader">
            <Setter Property="VerticalContentAlignment" Value="Bottom"/>
            <Setter Property="VerticalAlignment" Value="Bottom"/>
            <Setter Property="Height" Value="25"/>
        </Style>
        <Style x:Key="ModuleColumnHeaderStyle" TargetType="GridViewColumnHeader">
            <Setter Property="VerticalContentAlignment" Value="Bottom"/>
            <Setter Property="Width" Value="550"/>
        </Style>

        <helpers:FirstItemConverter x:Key="firstItemConvertion"/>

        <Style x:Key="ListViewItemRotatedText" TargetType="TextBlock">
            <Setter Property="LayoutTransform">
                <Setter.Value>
                    <RotateTransform Angle="-45" />
                </Setter.Value>
            </Setter>
            <Setter Property="Width" Value="130"/>
            <Setter Property="Padding" Value="0,0,0,0"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ModuleName, Converter={StaticResource firstItemConvertion}, ConverterParameter=0}" Value="true">
                        <Setter Property="Margin" Value="-60,0,0,0" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <DockPanel>
        <Grid DockPanel.Dock="Bottom" Margin="0,2,4,2">
            <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" VerticalAlignment="Center">
                <TextBlock Text="Selected Users: " />
                <ContentPresenter Content="{Binding Path=TotalSelectedUsers}" ContentStringFormat="0" />
                <TextBlock Text=" of " />
                <ContentPresenter Content="{Binding Path=TotalUsers}" ContentStringFormat="0" />
            </StackPanel>
        </Grid>
        <DataGrid AutoGenerateColumns="False" ItemContainerStyle="{StaticResource UserItemStyle}" AlternatingRowBackground="{x:Null}" DataContext="{StaticResource UserCollection}" ItemsSource="{Binding}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" IsReadOnly="True" HeaderStyle="{StaticResource ColumnHeaderStyle}" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Name}"/>
                <DataGridTextColumn Header="Job Title" IsReadOnly="True" HeaderStyle="{StaticResource ColumnHeaderStyle}" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Job_Title}"/>
                <DataGridTextColumn Header="Department" IsReadOnly="True" HeaderStyle="{StaticResource ColumnHeaderStyle}" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Department}"/>
                <DataGridTextColumn Header="Company" IsReadOnly="True" HeaderStyle="{StaticResource ColumnHeaderStyle}" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Company}"/>
                <DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=Company}" CanUserResize="False" Width="580">
                    <DataGridTextColumn.Header>
                        <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                            <TextBlock HorizontalAlignment="Center">Modules</TextBlock>
                            <ListBox x:Name="lstModules" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.Modules}">
                                <ListBox.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" CanVerticallyScroll="False" CanHorizontallyScroll="False"></StackPanel>
                                    </ItemsPanelTemplate>
                                </ListBox.ItemsPanel>
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Style="{StaticResource ListViewItemRotatedText}" Text="{Binding ModuleName}"></TextBlock>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </StackPanel>
                    </DataGridTextColumn.Header>
                </DataGridTextColumn>
                <DataGridTextColumn Header="Contact Detail" IsReadOnly="True" HeaderStyle="{StaticResource ColumnHeaderStyle}" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Name}"/>
            </DataGrid.Columns>
        </DataGrid>
    </DockPanel>
</UserControl>

[编辑] 在 DataGridColumnHeader 上放置背景颜色后

<Style x:Key="ColumnHeaderStyle" TargetType="DataGridColumnHeader">
    <Setter Property="VerticalContentAlignment" Value="Bottom"/>
    <Setter Property="VerticalAlignment" Value="Bottom"/>
    <Setter Property="Height" Value="25"/>
    <Setter Property="Background" Value="Tomato"/>
</Style>
<Style x:Key="ModuleColumnHeaderStyle" TargetType="DataGridColumnHeader">
    <Setter Property="Background" Value="Tomato"/>
</Style>

这就是它的样子:你可以看到白色/灰色的双色调仍然存在。 在此处输入图像描述

[EDIT2]要在第五列中获得相同的样式,请添加这些样式

<Style x:Key="ListViewItemRotatedText1" TargetType="TextBlock">
    <Setter Property="LayoutTransform">
        <Setter.Value>
            <RotateTransform Angle="-45" />
        </Setter.Value>
    </Setter>
    <Setter Property="Width" Value="130"/>
    <Setter Property="Margin" Value="0,0,0,0" />
</Style>
<Style x:Key="ListViewItemRotatedText2" TargetType="TextBlock">
    <Setter Property="LayoutTransform">
        <Setter.Value>
            <RotateTransform Angle="-45" />
        </Setter.Value>
    </Setter>
    <Setter Property="Width" Value="130"/>
    <Setter Property="Margin" Value="-60,0,0,0" />
</Style>

并将 ... 替换为以下 StackPanel

<StackPanel Orientation="Horizontal">
    <TextBlock Style="{StaticResource ListViewItemRotatedText1}" >Customer Services</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Asset Management</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Works Management</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Project Management</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Rates Management</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Finance</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Human Resources</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Document Management</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >User Management</TextBlock>
    <TextBlock Style="{StaticResource ListViewItemRotatedText2}" >Configuration</TextBlock>
</StackPanel>

[最后编辑] Viv 帮助下的结果 在此处输入图像描述

4

1 回答 1

4

更新:

<Style TargetType="DataGridColumnHeader">
  <Setter Property="Background"
          Value="Tomato" />
</Style>

^^ 不要指定 ax:Key我们试图让它到达虚拟 Header( PART_FillerColumnHeader),它应该跨越所有 Columns 并给出Background.

我们还需要ColumnHeaderStyle继承这个,所以我们最终会拥有,

<Style TargetType="DataGridColumnHeader">
  <Setter Property="Background"
          Value="Tomato" />
</Style>
<Style x:Key="ColumnHeaderStyle"
        BasedOn="{StaticResource {x:Type DataGridColumnHeader}}"
        TargetType="DataGridColumnHeader">
  <Setter Property="VerticalContentAlignment"
          Value="Bottom" />
  <Setter Property="VerticalAlignment"
          Value="Bottom" />
  <Setter Property="Height"
          Value="25" />
</Style>
于 2013-06-06T14:09:42.373 回答