<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/