我的App.xaml
文件中有一些样式:
<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>
我想更改BackgroundProperty
我的mainpage.xaml.cs
.
我尝试使用这个:
Style style = Application.Current.Resources["stackpanelBackground"] as Style;
style.Setters.SetValue(StackPanel.BackgroundProperty, "{StaticResource styleRed}");
但我得到一个灾难性的失败异常。我认为这与{StaticResource styleRed}
. 有一个更好的方法吗?