Create your own system colors

倾然丶 夕夏残阳落幕 提交于 2020-01-01 14:23:46

问题


Basically, how can I create my own set of Colors in a static class or the such so that I can do something like this:

What exists:

<Setter ... Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>

What I want:

<Setter ... Value="{DynamicResource {x:Static MyColors.Color1}}"/>

回答1:


Resource Keys can be anything, so you can use a Color as a key and value at the same time:

public static class MyColors
{
    static MyColors()
    {
        App.Current.Resources.Add(MyHighlightColorKey, MyHighlightColorKey);
    }

    public static readonly Color MyHighlightColorKey = Color.FromArgb(255, 0, 88, 0);
}

The static constructor adds the colour using itself as a key to the application resources.

(SystemColors uses SystemResourceKeys internally for every defined colour or brush, you have no access to that class however (which makes sense), alternatively you could subclass ResourceKey if you take issue with using the value as its own key)

You can use it like this:

<TextBox>
    <TextBox.Background>
        <SolidColorBrush Color="{DynamicResource {x:Static local:MyColors.MyHighlightColorKey}}"/>
    </TextBox.Background>
</TextBox>

And if you need to override the key on a local level you can do so as well:

<Window.Resources>
    <Color x:Key="{x:Static local:MyColors.MyHighlightColorKey}" A="255" R="255" G="0" B="0"/>
</Window.Resources>

Edit: If you have lots of colours, brushes and whatnot you could also use reflection to do the resource registering in the constructor (i used fields, if you use properties to expose the data you need to adjust this slightly):

static MyColors()
{
    FieldInfo[] keyFieldInfoArray = typeof(MyColors).GetFields();
    foreach (var keyFieldInfo in keyFieldInfoArray)
    {
        object value = keyFieldInfo.GetValue(null);
        App.Current.Resources.Add(value, value);
    }
}



回答2:


thought I'd chime in with another option. You can use a Static resource instead by doing something like the following...

public struct MyColors
{
    public static Brush Color1
    {
        get { return Brushes.Red; } // or whatever you like
    }
    public static Brush Color2
    {
        get { return Brushes.Blue; }
    }
}

Then in your XAML, use:

"{x:Static local:MyColors.Color1}"

I've just spent 10 minutes trying to get it to work with a DynamicResource extension, but I can't do it. If anyone knows how (or why) then let us know :)




回答3:


You can easily do this. You have to define the following class:

public class MyColors
{
    public static string Color1{ get{return "Color1Key";}}
}

and for example in your App.xaml you do:

<Application ...>
    <Application.Resources>
        <Color x:Key="Color1Key">#FF969696</Color>
    </Application.Resources>
</Application>

as the static string is actually just for strong typing I usually don't create such a static class and just use whatever key I defined so this becomes:

<Setter ... Value="{DynamicResource Color1Key}"/>

(I believe you can also use the strong typing in <Color x:Key="{x:Static MyColors.Color1}">#FF969696</Color> but I'm not sure right now...)

(also beware of that for using x:Static you will have to specify the namespace where MyColors sits so this becomes {x:Static local:MyColors.Color1})



来源:https://stackoverflow.com/questions/5490862/create-your-own-system-colors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!