【发布时间】:2015-01-22 06:46:40
【问题描述】:
我的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>
我想更改我的mainpage.xaml.cs 代码中的BackgroundProperty。
我试过用这个:
Style style = Application.Current.Resources["stackpanelBackground"] as Style;
style.Setters.SetValue(StackPanel.BackgroundProperty, "{StaticResource styleRed}");
但是我遇到了一个灾难性的失败异常。我认为这与{StaticResource styleRed} 有关。有没有更好的方法来做到这一点?
【问题讨论】:
标签: c# wpf xaml staticresource app.xaml