简体   繁体   English

WPF DataGrid行前景鼠标悬停颜色

[英]WPF DataGrid row foreground mouseover color

I have been messing around with datagrid styling for a couple of days now, and I am having one issue left which I can not seem to solve. 我已经纠缠了几天的datagrid样式,剩下一个我似乎无法解决的问题。

I want to highligt the text of the row on which I hover with the mouse. 我想高亮显示我用鼠标悬停的行的文本。 Well this seems like an easy task, but at the same time I want custom selection color of the row background and foreground. 好吧,这似乎是一项容易的任务,但同时我希望自定义选择颜色的行背景和前景。 This combination seems impossible for me to solve. 这种组合对我来说似乎无法解决。

If I override the datagridrow style I can get everything but the selection style on the row. 如果我重写了datagridrow样式,那么除了行上的选择样式外,我什么都可以得到。 If I also override the datagridcell style, I can get everything but the mouseover foreground full row highlight. 如果我也重写了datagridcell样式,则除了鼠标悬停前景全行突出显示之外,我可以得到所有内容。 What I have below is complete styling I have come up with so far. 我下面的内容是到目前为止我提出的完整样式。 I know it contains duplicate attributes for the same tasks, but I have been trying everything. 我知道它包含相同任务的重复属性,但是我一直在尝试一切。 Only the complete row mouseover highlight is missing - only the hovered cell is highlighted. 仅缺少整个行的鼠标悬停突出显示-仅悬停的单元格突出显示。

Can you help me solve this tiny detail? 您能帮我解决这个小细节吗?

Put the below code in a WPF Window (I know the coloring is awful, they are just for the example): 将以下代码放在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>

Put the below xml in a file called "datagrid.xml" in the same solution: 在相同的解决方案中,将以下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>

Thank you... 谢谢...

OK - my initial problem was, that I used a basestyle, which all other styles inherited from. 好的-我的最初问题是,我使用了基本样式,所有其他样式都继承自该样式。 This meant that styles such as Foreground was overriden multiple times through the style hierarchy. 这意味着诸如Foreground之类的样式在样式层次结构中被多次覆盖。 This messed thing up bad enough to make the above impossible. 这种混乱的事情足以使上述事情变得不可能。

The solution for me was to removed the BasedOn attribute of all datagrid styles except the DataGrid itself (datagridrowheader, datagridrow, datagridcell). 对我来说,解决方案是删除除DataGrid本身(datagridrowheader,datagridrow,datagridcell)以外的所有datagrid样式的BasedOn属性。 Set the foreground color only on the DataGrid and you can override it in the row and cell styles with no problem. 仅在DataGrid上设置前景色,并且可以在行和单元格样式中覆盖它而没有问题。

/mfas / mfas

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM