c# - 如何设置可用于 ResourceDictionary 中背景属性的原色

标签 c# .net wpf xaml mvvm

所以我创建了一个看起来像这样的ResourceDictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <SolidColorBrush x:Key="PrimaryColor" Color="#252525"/>
</ResourceDictionary>

现在我的问题是如何获得该 key 以便我可以将它用于我的 MainWindow 上的 background 属性?

<Window ...
        Background="{DynamicResource PrimaryColor}">

最佳答案

您需要将 ResourceDictionary 合并到您的 App.xaml 中:

<Application ...>
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="YourResourceDict.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

一旦它在范围内,您就可以使用 {DynamicResource key}{StaticResource key} 引用任何资源

What's the difference between StaticResource and DynamicResource in WPF?

关于c# - 如何设置可用于 ResourceDictionary 中背景属性的原色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55006298/

相关文章:

c# - 从 HTML 子类获取数据

c# - 如何使用 LINQ 进行简单的 OR 查询?

c# - 在我的构建输出中包含 Nuget 依赖项?

错误状态样式的 jQuery 交互提示

wpf - 有没有办法将 xaml.cs 中公共(public)方法的结果绑定(bind)到 xaml 中的控件?

javascript - 通过 Javascript 按 tab 键获取 selenium

c# - 在 .NET 中获取原始(未拆分)命令行

c# - DynamicTableEntity 类有何相关性?

c# - 嵌套用户控件的数据上下文

C# INotifyPropertyChanged 处理程序