开发者

WPF generic colors based on SystemColors

开发者 https://www.devze.com 2022-12-25 15:56 出处:网络
I need to have some generic colors in my application, which are based on the system colors, which change based on the active theme. So I believe I need 开发者_运维问答some IValueConverters that each r

I need to have some generic colors in my application, which are based on the system colors, which change based on the active theme. So I believe I need 开发者_运维问答some IValueConverters that each return a brush when given a system color brush.

But where do I put the logic? As I see it I might have 2 options.

Option 1) place SolidColorBrush(es) in the themed resource dictionary, which bind to some system colors and converts them into new Brush(es). This I have tried and it seems to work, but how do I reference these new SolidColorBrush(es) from code-behind.

Option 2) create a class similar to SystemColors class. I don't quite know how I just go about doing this. What is the relation between the SystemResourceKey and the Brush in the SystemColors class? How are they connected?

Best Regards, Jesper


1. e.g.

<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 can probably reuse the keys from the SystemColors class to build your dictionary, it does not really matter what you use, a key can be any object, this question might be of interest.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号