C# WPF使用资源中的文本颜色
我有一个名为colors.xaml的带有颜色的文件C# WPF使用资源中的文本颜色,c#,wpf,resourcedictionary,C#,Wpf,Resourcedictionary,我有一个名为colors.xaml的带有颜色的文件 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ErgoRythm">
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ErgoRythm">
<Color x:Key="TextColor1">#696969</Color>
</ResourceDictionary>
#696969
在我的App.xaml中,我有
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="CustomStyles" Source="Colors.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
现在我想将颜色添加到文本中,但我得到一个“资源”TextColor1“具有不兼容的类型。”
<Label Grid.Row="0" Content="Genearal Volume" Grid.Column="0" FontSize="20" Foreground="{DynamicResource TextColor1}" />
如注释
中所述,前景是一种SolidColorBrush
而不是一种颜色。因此,改变:
#696969
为此:
前景是画笔,而不是颜色。因此,将696969
或
作为背景信息,之所以存在颜色
和SolidColorBrush
是因为可以使用画笔的属性可以使用不同类型的画笔。因此,可以用不仅仅是简单的颜色来填充前景
和背景
(以及其他颜色)。添加到这里,您可以考虑保留<代码>颜色< /代码>资源,并添加使用< <代码>颜色< /代码>的<代码> SolidColorBrush < /代码>资源。在设计应用程序主题时,有时值得使用少量(3-5)颜色作为应用程序范围的调色板,然后创建一些专门构建的应用程序笔刷,仅使用这些3-5颜色。