WPF DataGrid行前景鼠标悬停颜色

时间:2012-01-17 06:27:50

标签: c# .net wpf datagrid styling

我一直在搞乱数据网格样式几天,我还有一个问题,我似乎无法解决。

我想高举我用鼠标悬停的行的文本。嗯,这似乎是一个简单的任务,但同时我想要行背景和前景的自定义选择颜色。这种组合似乎不可能让我解决。

如果我覆盖datagridrow样式,我可以获得除行上选择样式之外的所有内容。如果我也覆盖datagridcell样式,我可以获得除鼠标悬停前景全行突出显示之外的所有内容。我所拥有的是迄今为止我想出的完整造型。我知道它包含相同任务的重复属性,但我一直在尝试一切。仅缺少完整的行鼠标悬停突出显示 - 仅突出显示悬停的单元格。

你能帮我解决这个微小的细节吗?

将以下代码放在WPF窗口中(我知道着色很糟糕,它们仅用于示例):

<Window.Resources>
    <XmlDataProvider x:Key="persondata"
                     Source="datagrid.xml"
                     XPath="Data" />

    <!-- DataGrid -->
    <Style x:Key="{x:Type DataGrid}"
           TargetType="{x:Type DataGrid}">
        <Setter Property="AlternatingRowBackground"
                Value="Orange" />
        <Setter Property="AlternationCount"
                Value="2" />
        <Setter Property="Background"
                Value="Green" />
        <Setter Property="BorderThickness"
                Value="0" />
        <Setter Property="Foreground"
                Value="Cyan" />
        <Setter Property="HeadersVisibility"
                Value="Column" />
        <Setter Property="RowBackground"
                Value="Yellow" />
    </Style>

    <!-- Column header - remove header background -->
    <Style x:Key="{x:Type DataGridColumnHeadersPresenter}"
           TargetType="{x:Type DataGridColumnHeadersPresenter}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeadersPresenter}">
                    <ItemsPresenter />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!-- Column header sort arrow -->
    <Style x:Key="ColumnHeaderSortArrowStyle"
           TargetType="{x:Type Path}">
        <Setter Property="Data"
                Value="M0,0 L1,0 0.5,1 z" />
        <Setter Property="Fill">
            <Setter.Value>
                <SolidColorBrush Color="Blue" />
            </Setter.Value>
        </Setter>
        <Setter Property="Height"
                Value="6" />
        <Setter Property="Margin"
                Value="0,0,8,0" />
        <Setter Property="RenderTransformOrigin"
                Value="0.5,0.4" />
        <Setter Property="Stretch"
                Value="Fill" />
        <Setter Property="VerticalAlignment"
                Value="Center" />
        <Setter Property="Visibility"
                Value="Collapsed" />
        <Setter Property="Width"
                Value="8" />
    </Style>

    <!-- Column header gripper -->
    <Style x:Key="ColumnHeaderGripperStyle"
           TargetType="{x:Type Thumb}">
        <Setter Property="Width"
                Value="8" />
        <Setter Property="Background"
                Value="Transparent" />
        <Setter Property="Cursor"
                Value="SizeWE" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Thumb}">
                    <Border Background="{TemplateBinding Background}"
                            Padding="{TemplateBinding Padding}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!-- Column header -->
    <Style x:Key="{x:Type DataGridColumnHeader}"
           TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                    <Border BorderThickness="0,1,1,0"
                            Background="Black"
                            CornerRadius="5,5,0,0">
                        <Border.BorderBrush>
                            <SolidColorBrush Color="Red" />
                        </Border.BorderBrush>

                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>

                            <Thumb x:Name="PART_LeftHeaderGripper"
                                   Grid.Column="0"
                                   Grid.Row="0"
                                   HorizontalAlignment="Left"
                                   Style="{StaticResource ColumnHeaderGripperStyle}" />

                            <ContentPresenter Grid.Column="0"
                                              Grid.Row="0"
                                              Margin="6,3,6,3"
                                              VerticalAlignment="Center" />

                            <Path x:Name="SortArrow"
                                  Grid.Column="1"
                                  Grid.Row="0"
                                  Style="{StaticResource ColumnHeaderSortArrowStyle}">
                            </Path>

                            <Thumb x:Name="PART_RightHeaderGripper"
                                   Grid.Column="1"
                                   Grid.Row="0"
                                   HorizontalAlignment="Right"
                                   Style="{StaticResource ColumnHeaderGripperStyle}" />
                        </Grid>
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver"
                                 Value="True">
                            <Setter TargetName="SortArrow"
                                    Property="Fill">
                                <Setter.Value>
                                    <SolidColorBrush Color="Blue" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>

                        <Trigger Property="SortDirection"
                                 Value="Ascending">
                            <Setter TargetName="SortArrow"
                                    Property="Visibility"
                                    Value="Visible" />
                            <Setter TargetName="SortArrow"
                                    Property="RenderTransform">
                                <Setter.Value>
                                    <RotateTransform Angle="180" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>

                        <Trigger Property="SortDirection"
                                 Value="Descending">
                            <Setter TargetName="SortArrow"
                                    Property="Visibility"
                                    Value="Visible" />
                        </Trigger>

                        <Trigger Property="DisplayIndex"
                                 Value="0">
                            <Setter Property="Visibility"
                                    Value="Collapsed"
                                    TargetName="PART_LeftHeaderGripper"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver"
                     Value="True">
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="Blue" />
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

    <!-- Row -->
    <Style x:Key="{x:Type DataGridRow}"
           TargetType="{x:Type DataGridRow}">
        <Style.Triggers>
            <Trigger Property="ItemsControl.AlternationIndex"
                     Value="0">
                <Setter Property="Background"
                        Value="Yellow" />
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="Black" />
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="ItemsControl.AlternationIndex"
                     Value="1">
                <Setter Property="Background"
                        Value="Orange" />
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="Black" />
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsMouseOver"
                     Value="true">
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="Magenta" />
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsSelected"
                     Value="True">
                <Setter Property="Background"
                        Value="Brown" />
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="White" />
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

    <!-- Cell -->
    <Style x:Key="{x:Type DataGridCell}"
           TargetType="{x:Type DataGridCell}">
        <Setter Property="Foreground">
            <Setter.Value>
                <SolidColorBrush Color="Black" />
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver"
                     Value="true">
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="Magenta" />
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsSelected"
                     Value="True">
                <Setter Property="Background"
                        Value="Brown" />
                <Setter Property="BorderBrush"
                        Value="Brown" />
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="White" />
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <DataGrid AutoGenerateColumns="False"
              ItemsSource="{Binding Source={StaticResource persondata}, XPath=Person}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="First Name"
                                Binding="{Binding XPath=@Firstname}" />
            <DataGridTextColumn Header="Last Name"
                                Binding="{Binding XPath=@Lastname}" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>

将以下xml放在同一解决方案中名为“datagrid.xml”的文件中:

<?xml version="1.0" encoding="utf-8" ?>
<Data>
    <Person Firstname="Fred"
            Lastname="Flintstone">
    </Person> 
    <Person Firstname="Barney"
            Lastname="Rubble">
    </Person> 
    <Person Firstname="Joe"
            Lastname="Rockhead">
    </Person> 
</Data>

谢谢...

1 个答案:

答案 0 :(得分:0)

好的 - 我最初的问题是,我使用了basestyle,其他所有样式都继承自。这意味着通过样式层次结构多次覆盖Foreground等样式。这件搞糟的事情已经不足以使上述事情变得不可能了。

我的解决方案是删除除DataGrid本身(datagridrowheader,datagridrow,datagridcell)之外的所有数据网格样式的BasedOn属性。仅在DataGrid上设置前景色,您可以在行和单元格样式中覆盖它而没有任何问题。

/ MFAS