简体   繁体   中英

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?

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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