创建自己的系统颜色 [英] Create your own system colors
问题描述
基本上,我如何在静态类中创建自己的颜色集合,以便我可以这样做:
什么存在:
< Setter ... Value ={DynamicResource {x:Static SystemColors.ControlBrushKey}} />
我想要什么:
< Setter ... Value ={DynamicResource {x:Static MyColors.Color1}}/>
资源键可以是任何东西, Color
作为键和值的同时:
public static class MyColors
{
static MyColors()
{
App.Current.Resources.Add(MyHighlightColorKey,MyHighlightColorKey);
}
public static readonly Color MyHighlightColorKey = Color.FromArgb(255,0,88,0);
}
静态构造函数使用自身作为应用程序资源的键来添加颜色。
( SystemColors
在内部使用 SystemResourceKeys
每一个定义的颜色或画笔,你没有访问那个类(这是有道理的),或者你可以子类 ResourceKey
如果你把这个值作为自己的键
您可以这样使用:
< TextBox>
< TextBox.Background>
< SolidColorBrush Color ={DynamicResource {x:Static local:MyColors.MyHighlightColorKey}}/>
< /TextBox.Background>
< / TextBox>
如果你需要覆盖本地关键字,你也可以这样做: p>
< Window.Resources>
< Color x:Key ={x:Static local:MyColors.MyHighlightColorKey}A =255R =255G =0B =0
< /Window.Resources>
编辑:你有很多颜色,画笔,你也可以使用反射做资源注册的构造函数(我使用的字段,如果你使用属性来公开你需要稍微调整这些数据):
static MyColors()
{
FieldInfo [] keyFieldInfoArray = typeof(MyColors).GetFields();
foreach(keyFieldInfoArray中的var keyFieldInfo)
{
对象值= keyFieldInfo.GetValue(null);
App.Current.Resources.Add(value,value);
}
}
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}}"/>
解决方案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
usesSystemResourceKeys
internally for every defined colour or brush, you have no access to that class however (which makes sense), alternatively you could subclassResourceKey
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); } }
这篇关于创建自己的系统颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!