Sunday, 15 September 2013

How do I access a resource(style) through code?

How do I access a resource(style) through code?

I have a Resource Dictionary containing all my custom styles for the
programs controls. The Dictionary is mergerd with the application's
resources as displayed below:
<ResourceDictionary x:Key="Controls">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Controls.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
I can easily access the different styles through xaml:
<Button Style="{StaticResource Button}" />
But whenever I try assigning controls with this style through code, it fails.
I've tried:
Button.Style = Application.Current.Resources("Button")
Button.Style = CType(Application.Current.Resources("Button"), Style)
And different approaches similar to the ones above. During testing some of
the different ways to get the styles, I was faced with "Resource not
found" but when using the above ones the program seemed to find the style.
I could successfully run the program - but without any visual proof that
the style was indeed applied.
How do I properly assign a control a style found in a Resource Dictionary?

No comments:

Post a Comment