簡體   English   中英

如何基於Visual Studio主題設置WPF DataGrid的非活動行顏色

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

我正在研究VSIX(Visual Studio擴展)項目。 它包含一個WPF DataGrid。 當DataGrid變為非活動狀態(失去焦點)時,我需要基於Visual Studio主題設置自定義DataGrid行突出顯示顏色

盡管通過堆棧溢出發現了許多類似的問答,但我無法找到基於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>

謝謝

您可以嘗試使用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>

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM