Search code examples
c#wpfvisual-studiovsixvs-extensibility

How to apply current VS theme to existing controls


The app I'm working on has a desktop version as well as a Visual Studio extension version, both roughly providing the same functionality, and therefore reusing the same set of WPF controls. These controls reside in a WPF Controls Library. All was working just as expected.

Recently I discovered that the extension doesn't look very nice if I switch my Visual Studio to the dark theme. The solution as I googled was to use Visual Studio's built-in brushes and colors.

How do I do that? My controls have deeply nested styles and colors. If I create additional styles in VS extension, WPF will still apply the local brushes and values defined inside the controls. How to get around this problem?


Solution

  • You can use Visual Studio's brushes and colors like this:

    <UserControl ... 
        xmlns:vsShell="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Shell.14.0">
    
    <TextBox Foreground="{DynamicResource {x:Static vsShell:EnvironmentColors.ToolWindowTextBrushKey}}" />
    

    You could also check which VS theme is active using a ThemeResourceKey which has different colors in all the VS themes (like AccentBorderColorKey) and check which color is returned.

    var color = VSColorTheme.GetThemedColor(EnvironmentColors.AccentBorderColorKey);
    

    enter image description here

    And assign your own (better looking) colors to your WPF controls.

    Documentation:

    ThemeResouceKey

    VSColorTheme.GetThemedColor

    Extra:

    This might be helpful in selecting the right ThemeResourceKey

    VS Colors