Common WPF colors based on SystemColors

I need to have some common colors in my application that are based on system colors that change depending on the active theme. Therefore, I believe that I need some IValueConverters, each of which returns a brush when setting a color brush for the system.

But where can I put the logic? As I can see, I can have 2 options.

Option 1) put SolidColorBrush (es) in a thematic resource dictionary that binds to some system colors and transforms them into new brushes. I tried it and it seems to work, but how do I bind these new SolidColorBrush from code.

Option 2) create a class similar to the SystemColors class. I don’t quite understand how I do it. What is the relationship between SystemResourceKey and Brush in the SystemColors class? How are they connected?

Regards, Jesper

+3
source share
1 answer

1. for example

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:Test">
    <SolidColorBrush x:Key="Brush1" Color="Green"/>
</ResourceDictionary>
ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("pack://application:,,,/TestDictionary.xaml");
Brush brush1 = dict["Brush1"] as SolidColorBrush;

2. You cannot use SystemResourceKeys, they are internal, but you can probably reuse the keys from the SystemColors class to create your dictionary, it really does not matter what you use, the key can be any object, this question may be of interest.

0
source

Source: https://habr.com/ru/post/1739119/


All Articles