c# - 如何在代码隐藏中创建 ResourceDictionary?

标签 c# wpf xaml .net-4.0 resourcedictionary

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <sys:String x:Key="one">ONE</sys:String>
    <sys:String x:Key="two">TWO</sys:String>
    <sys:String x:Key="three">THREE</sys:String>
</ResourceDictionary>

现在想使用 C# 在 WPF 中通过 code-behind 动态创建与上面相同的资源 ResourceDictionary。是否可以这样创建?

最佳答案

public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainViewModel();
        ResourceDictionary rd = new ResourceDictionary();
        rd.Add("one", "ONE");
        rd.Add("two", "TWO");
        rd.Add("three", "THREE");
        this.Resources.MergedDictionaries.Add(rd);
    }

希望对您有所帮助。

关于c# - 如何在代码隐藏中创建 ResourceDictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15182396/

相关文章:

c# - 我如何在 C# 中使用 32 位 unicode 字符?

c# - 使用特定方法套接字服务器调用

WPF:隔离存储文件路径太长

c# - 单击 Xamarin Forms 中的提交按钮后如何从 XAML 获取值?

wpf - datatrigger 中的反向动画

c# - 系统线程计时器

c# - 在不同 DPI 设置下运行的 Windows 窗体应用程序中的奇怪行为

wpf - 在 ItemsControl 中虚拟化高度为 * 的 ItemsControl

c# - Visual Studio 2012 中的 MVVM Light 不显示项目模板

c# - 如何将一个控件的属性绑定(bind)到另一个控件?