Kamilos
Kamilos

Reputation: 805

WPF reading Style from ResourceDictionary to Control in C# code

I have code that looks like below:

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

VS2008 retuns an error:

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

Cannot implicitly convert type 'System.Windows.Style' to 'System.Windows.ResourceDictionary'

How I can properly assign a style from ResourceDictionary to control?

Upvotes: 4

Views: 7256

Answers (1)

Jarek Kardas
Jarek Kardas

Reputation: 8455

it should be as easy as

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

Upvotes: 5

Related Questions