WPF使用资源中的文本颜色

问题描述 投票:0回答:1

我有一个颜色称为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">
    <Color x:Key="TextColor1">#696969</Color>
</ResourceDictionary> 

在我的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}" />
c# wpf resourcedictionary
1个回答
0
投票

如注释中所述,ForegroundSolidColorBrush不是颜色。因此更改:

<Color x:Key="TextColor1">#696969</Color>

对此:

<SolidColorBrush x:Key="TextColor1" Color="#696969"/>
© www.soinside.com 2019 - 2024. All rights reserved.