【问题标题】:How to set WPF DataGrid's inactive row color based on Visual Studio Theme如何基于 Visual Studio Theme 设置 WPF DataGrid 的非活动行颜色
【发布时间】:2017-12-01 18:10:23
【问题描述】:

我正在开发一个 VSIX(Visual Studio 扩展)项目。它包含一个 WPF 数据网格。我需要设置自定义 DataGrid 行突出显示颜色,当 DataGrid 变为非活动状态(失去焦点)时,基于 Visual Studio 主题。

虽然通过堆栈溢出找到了许多类似的问答,但我无法找到基于 Visual Studio 主题的解决方案。

我通过以下代码(样式)遇到了。但是它不满足我的要求,请帮助解决这个问题。

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

谢谢

【问题讨论】:

    标签: c# wpf visual-studio-extensions vsix


    【解决方案1】:

    您可以尝试使用VsBrushesEnvironmentColors 来实现。像这样:

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

    【讨论】:

    • 听到的问题是基于 Rawbackground 的 VsBrush 颜色,我需要应用/设置非活动行突出显示颜色。换句话说,如果(Vs 主题 = 深色)则非活动行背景 = 红色,如果(Vs 主题 = 浅)则非活动行背景 = 绿色谢谢
    猜你喜欢
    • 2017-06-16
    • 2016-05-03
    • 2015-10-18
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 2010-09-27
    • 2018-01-16
    相关资源
    最近更新 更多