在C#代码中从ResourceDictionary读取样式并应用于控件的WPF技术

4

我有一段代码如下:

ResourceDictionary res = (ResourceDictionary)Application.LoadComponent(new Uri("Style.xaml", UriKind.Relative));
Style style = new Style();
style.Resources = (Style)res["ComboBoxTextBox"];

VS2008返回一个错误:

style.Resources = (样式)res["ComboBoxTextBox"];


(将“Style”翻译为“样式”,因为这更符合中文语境。)
Cannot implicitly convert type 'System.Windows.Style' to 'System.Windows.ResourceDictionary'

我该如何将ResourceDictionary中的样式正确地分配给控件?

1个回答

5

这应该像以下这样简单:

myControl.Style =  (Style)res["ComboBoxTextBox"];

啊,是的,我刚刚犯了一个简单的错误 :) 今天工作太辛苦了 :D 谢谢你的帮助。 - Kamilos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接