我将为一些控件创建一个简单的主题。我现在的问题是从第一ResourceDictionary到第二ResourceDictionary的访问权限。
示例: ResourceDictionary1 (DicColors)包含颜色,而第二个ResourceDictionary ResourceDictionary需要来自DicColors的颜色来构建SolidColorBrush:
DicColors
<Color x:Key="Color_System_Green">##8ec760</Color>
<Color x:Key="Color_System_Blue">#53baf2</Color>
<Color x:Key="Color_System_Orange">#ff9a1e</Color> DicThemeColor
<SolidColorBrush x:Key="Button.Static.Background" Color="{StaticResource Color_System_Green}"/>
<SolidColorBrush x:Key="Button.MouseOver.Background Color="{StaticResource Color_System_Blue}"/>
<SolidColorBrush x:Key="Button.Pressed.Background" Color="{StaticResource Color_System_Orange}"/> 目标是,在单个ResourceDictionary中定义颜色,其他ResourceDictionaries可以使用这种颜色。我现在的问题是,我没有机会从DicThemeColor到DicColors。
我会改变背景。这里是按钮默认模板中的一个片段.在App.xaml中,我合并了ResourceDictionaries。
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/TestApp;component/Styles/Colors/DicColors.xaml" />
<ResourceDictionary Source="pack://application:,,,/TestApp;component/Styles/Controls/DicThemeColor.xaml" />
</ResourceDictionary.MergedDictionaries>这里是按钮的默认模板。我只改变了背景。您可以在这里看到最后一行中的更改。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestApp.Styles.Controls">
<Style x:Key="FocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="Button.Static.Background" Color="{DynamicResource Color.Button.Static.Background}"/>当我使用这种样式的按钮,我得到一个白色的背景,而不是绿色。我不知道为什么。
希望你能帮我。
问候
发布于 2020-12-20 21:16:05
我认为您需要将对两个资源字典的引用更改为:
<ResourceDictionary Source="pack://application:,,,/Styles/Colors/DicColors.xaml" />
<ResourceDictionary Source="pack://application:,,,/Styles/Controls/DicThemeColor.xaml" /> 不知道您的文件夹结构,您可能需要调整这一点。
https://stackoverflow.com/questions/65378982
复制相似问题