I am new to Silverlight, but I couldn’t find anything about this when I googled it.
I have a button that I am trying to set the style programatically. I have the style defined in a XAML file and I want to pull the style into C# so I can dynamically create a button and assign it this style. So far, this is what I am trying:
button.Style = (Style)Resources["CloseButtonStyle"];
However, it just makes the button have no style. Is there an easy way to do this? I feel like this should be obvious, but I can’t get it to work.
You are assuming that your
Resourcesproperty on the current object is the one that contains the defined style. However, I assume, given the symptoms of your issue, thatCloseButtonStyleis actually defined further up the control hierarchy.Instead, you need to traverse your control hierarchy until you find the resource (or if you know the object that defines it, just refer directly to that object). Unfortunately, Silverlight doesn’t include
FindResourcecall like WPF, but it’s not too difficult to implement your own.