Stavros Koureas
Stavros Koureas

Reputation: 1482

change style-setter-property

I have the code below is very simple with style resources in wp7,

<Style x:Name="image_find" x:Key="ImageFind1" TargetType="Image">
     <Setter Property="Source" Value="display/pen.png"/>
</Style>

I want to change the source value of setter, for ex. "display/tool.png" when I want beside runtime in code of my app, take in mind that I need image beeing in style :)

I am running something like this,

image_find.Setters.SetValue(Image.SourceProperty, "display/tool.png");

or something like,

style = App.Current.Resources["image_find1"] as Style;    
style.Setters.SetValue(Image.SourceProperty, "display/tool.png");`

and I am getting NullReferenceException and the app breaks...

Upvotes: 0

Views: 1316

Answers (1)

sa_ddam213
sa_ddam213

Reputation: 43596

It really depends on where your Style is located, and you need to use the x:Key to find the Style not the x:Name

If the style is in your Application Resources (App.xaml) this should work

   var style = App.Current.Resources["ImageFind1"] as Style;

If its within the context of your Window/UserControl you will use FindResource

   var style = FindResource("ImageFind1") as Style;

Upvotes: 2

Related Questions