WPF 在 C# 代码中从 ResourceDictionary 读取 Style 到 Control

Kam*_*los 4 c# wpf resources

我的代码如下所示:

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

VS2008返回错误:

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

Cannot implicitly convert type 'System.Windows.Style' to 'System.Windows.ResourceDictionary'
Run Code Online (Sandbox Code Playgroud)

如何正确分配控件的样式ResourceDictionary

Jar*_*das 5

它应该像这样简单

myControl.Style =  (Style)res["ComboBoxTextBox"];
Run Code Online (Sandbox Code Playgroud)