Alex
Alex

Reputation: 626

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?

Upvotes: 3

Views: 7472

Answers (3)

Alex SSB
Alex SSB

Reputation: 1

My issue was using nested dictionaries, where accessing the resource directly by using Application.Current.Resources[] or App.Current.FindResource() didn't work.

I had to specify the specific dictionary to be able to access the resource:

((ResourceDictionary)Application.Current.Resources["ResourceDictionary"])["StaticResource"]

Upvotes: 0

Alex
Alex

Reputation: 626

For any descendants: here is how I succeded to apply a style from a resource to a dynamically created control through code. (Given that you have a Resource Dictionary containing the style)

First step: Include the Resource Dictionary

To make a Resource Dictionary easily accessible from code, add it through code.

VB

  Dim myResourceDictionary As New ResourceDictionary
  myResourceDictionary .Source = New _
  Uri("/YourApplication;component/YourDictionary.xaml",
        UriKind.RelativeOrAbsolute)

C#

   var myResourceDictionary = new ResourceDictionary
       {
           Source = new Uri("/YourApplication;component/YourDictionary.xaml", UriKind.RelativeOrAbsolute)
       };

Replace "YourApplication" with your solution name, and "YourDictionary" with your Resource Dictionary file.

Second step: Assign the Style

To make use of the newly imported Resource Dictionary, simply assign a control a style;

VB

  Dim myButton As New Button
  Dim myButtonStyle As Style = myResourceDictionary("YourStyleKey")
  myButton.Style = myButtonStyle

C#

  var myButtonStyle= myResourceDictionary["YourStyleKey"] as Style;
  var myButton = new Button { Style = myButtonStyle };

Special thanks to user Stefan Denchev for giving me an article covering this. As C# isn't my strong side, please edit this if I've made any mistake.

Upvotes: 6

user786981
user786981

Reputation:

Use Application.Current.Resources["Button"].

Upvotes: 5

Related Questions