How do I reference a StaticResource in code-behind?

Jerry Nixon picture Jerry Nixon · Jul 1, 2015 · Viewed 15.7k times · Source

In XAML I do it like this:

<Button Style="{StaticResource NavigationBackButtonNormalStyle}" />

How do I do the same thing in code-behind?

Answer

Jerry Nixon picture Jerry Nixon · Jul 1, 2015

The page-level Resources object has the ability to find local, app-level, static, and theme resources. This means you simply do this:

foo2.Style = this.Resources["NavigationBackButtonNormalStyle"] as Style;

Best of luck!