I have a few styles in my App.xaml
file:
<SolidColorBrush x:Key="styleBlue" Color="#FF4B77BE"/> <SolidColorBrush x:Key="styleRed" Color="#FFF64747"/> <SolidColorBrush x:Key="styleOrange" Color="#FFF89406"/> <SolidColorBrush x:Key="styleGreen" Color="#FF1BBC9B"/> <SolidColorBrush x:Key="styleYellow" Color="#FFF9BF3B"/> <Style x:Key="stackpanelBackground" TargetType="StackPanel"> <Setter Property="Background" Value="{StaticResource styleBlue}"/> </Style>
I want to change the BackgroundProperty
in the code of my mainpage.xaml.cs
.
I tried using this:
Style style = Application.Current.Resources["stackpanelBackground"] as Style; style.Setters.SetValue(StackPanel.BackgroundProperty, "{StaticResource styleRed}");
But I get a catastrophic failure exception. I think it has to do with {StaticResource styleRed}
. Is there a better way to do this?
Static resources allow you to upload content that you can reference in a Visualforce page, including archives (such as . zip and . jar files), images, style sheets, JavaScript, and other files. Static resources can be used only within your Salesforce org, so you can't host content here for other apps or websites.
Static = no change, therefor you cannot change a Static resource. A static resource has no mechanism to notify the UI if a change is made making it quicker to use as the value will not change. Static = no change, therefor you cannot change a Static resource.
Click New Static Resource to define a new static resource. Click a resource name to display detailed information about the page, including its MIME type and size. Click Edit next to a resource to modify the resource's name or to upload a new version of the resource. Click Del to remove a resource.
A StaticResource
is static. You can't change them once the application has compiled.
For this purpose, there is DynamicResource
:
A DynamicResource will create a temporary expression during the initial compilation and thus defer lookup for resources until the requested resource value is actually required in order to construct an object.
Also note that you can find the reference to the other resource better using FindResource
. Try something like this (full working sample):
In MainPage.xaml
:
<Window.Resources> <Color R="255" x:Key="styleRed" /> <Style x:Key="abc" TargetType="StackPanel"> <Setter Property="Background" Value="Blue" /> </Style> </Window.Resources>
In MainPage.xaml.cs
:
Style style = this.FindResource("abc") as Style; var r = this.FindResource("styleRed"); foreach (Setter s in style.Setters) { if (s.Property == StackPanel.BackgroundProperty) { s.Value = r; } }
Why are you modifying the Style instead of setting the Background
-Property of your targeted StackPanel
directly? Since a 'Local value' has a higher precedence than 'Style setters' the value that you write into Background
from code behind would be used
Means:
(1) Give a name to your stackpanel x:Name="spBla"
(2) Assign the Brush to the Background
of spBla
( something like spBla.Background=Application.Current.Resources["styleRed"] as SolidColorBrush;
)
You can learn more about value precedence here:
http://msdn.microsoft.com/en-us/library/ms743230(v=vs.110).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With