Setting the Style property of a WPF Label in code?
Where in code are you trying to get the style? Code behind?
You should write this:
If you're in code-behind:
Style style = this.FindResource("LabelTemplate") as Style;label1.Style = style;
If you're somewhere else
Style style = Application.Current.FindResource("LabelTemplate") as Style;label1.Style = style;
Bottom note: don't name a Style
with the keyword Template
, you'll eventually end up confusing a Style
and a Template
, and you shouldn't as those are two different concepts.
Maybe an old question, but if you are trying W10 UWP app must use resources collection of each object or resources collection of Application object
KeyValuePair<object,object> styl = this.Resources .Where(x => x.Key.ToString() == "MyStyleTemplateName") .FirstOrDefault();if (styl.Value != null) Style MyStyle = (Style)styl.Value;
Where MyStyleTemplateName must be defined as a resource of this