简体   繁体   English

如何基于Visual Studio主题设置WPF DataGrid的非活动行颜色

[英]How to set WPF DataGrid's inactive row color based on Visual Studio Theme

I'm working on a VSIX (Visual Studio Extension) project. 我正在研究VSIX(Visual Studio扩展)项目。 It contains a WPF DataGrid. 它包含一个WPF DataGrid。 I need to set a custom DataGrid row highlight color, when DataGrid becomes inactive (lost focus), based on Visual Studio Theme. 当DataGrid变为非活动状态(失去焦点)时,我需要基于Visual Studio主题设置自定义DataGrid行突出显示颜色

Although many similar kind of Q&As are found via Stack overflow I'm unable to find a solution which is based on Visual studio theme. 尽管通过堆栈溢出发现了许多类似的问答,但我无法找到基于Visual Studio主题的解决方案。

I have came across through the following piece of code (style). 我遇到了以下代码(样式)。 However it doesn't cover my requirement, Please help to resolve this issue. 但是,这不能满足我的要求,请帮助解决此问题。

 <DataGrid.CellStyle>
            <Style TargetType="{x:Type DataGridCell}">
                <Style.Triggers>
                    <Trigger  Property="IsSelected" Value="true">
                        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                    </Trigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True" />
                            <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=IsKeyboardFocusWithin}" Value="False" />
                        </MultiDataTrigger.Conditions>

                        <MultiDataTrigger.Setters>

                            <!--Following Background color has to be changed based on VS theme-->
                            <Setter Property="Background" Value="DarkGray" />

                            <Setter Property="Foreground" Value="{DynamicResource VsBrush.WindowText}" />

                            <!--This BorderBrush color has to be changed based on VS theme-->
                            <Setter Property="BorderBrush" Value="DarkGray"/>
                        </MultiDataTrigger.Setters>
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>

Thanks 谢谢

you can try to use VsBrushes or EnvironmentColors to achieve it. 您可以尝试使用VsBrushesEnvironmentColors来实现。 like this: 像这样:

<UserControl x:Class="TWToolbar.TestToolWindowControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:vsShell="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.14.0"
             xmlns:vsUI="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Shell.14.0"

             mc:Ignorable="d"
             d:DesignHeight="400" d:DesignWidth="500"
             Name="MyToolWindow">
    <Grid>
        <StackPanel Orientation="Vertical">
            <Button Content="Click me!" Click="button1_Click" Width="120" Height="20" Name="button1"/>
            <DataGrid Name="dgUsers" AutoGenerateColumns="False">
            <DataGrid.CellStyle>
                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <Trigger  Property="IsSelected" Value="true">
                            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" />
                            <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                        </Trigger>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True" />
                                <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=IsKeyboardFocusWithin}" Value="False" />
                            </MultiDataTrigger.Conditions>
                            <MultiDataTrigger.Setters>
                                <!--Following Background color has to be changed based on VS theme WindowText-->
                                <Setter Property="Background" Value="DarkGray" />
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static vsShell:VsBrushes.AccentBorderKey}}"/>
                                <!--<Setter Property="Foreground" Value="{DynamicResource {x:Static vsUI:EnvironmentColors.AccentBorderBrushKey}}"/>-->
                                <!--This BorderBrush color has to be changed based on VS theme-->
                                <Setter Property="BorderBrush" Value="DarkGray"/>
                            </MultiDataTrigger.Setters>
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.CellStyle>
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                    <DataGridTextColumn Header="Birthday" Binding="{Binding Birthday}" />
                </DataGrid.Columns>
            </DataGrid>
        </StackPanel>
    </Grid>
</UserControl>

在此处输入图片说明

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

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