WPF 精修篇 动态资源

原文:WPF 精修篇 动态资源

动态资源 使用 DynamicResource 关键字

静态 就是 StaticResource

原则上是 能用静态就用静态 动态会让前台界面压力很大~

 

动态资源引用 就是可以在后台改变资源 显示不同的样式  资源是一样的 就看关键字用什么

效果 

WPF 精修篇 动态资源

 

  1. <Window.Resources>
  2. <LinearGradientBrush x:Key="RectFill" EndPoint="0.5,1" StartPoint="0.5,0">
  3. <GradientStop Color="BurlyWood" Offset="0"/>
  4. <GradientStop Color="White" Offset="1"/>
  5. </LinearGradientBrush>
  6. </Window.Resources>
  7. <Grid>
  8. <Rectangle Fill="{ DynamicResource RectFill}" HorizontalAlignment="Left" Height="76" Margin="85,70,0,0" Stroke="Black" VerticalAlignment="Top" Width="243">
  9. </Rectangle>
  10. <RadioButton x:Name="R" Content="R" HorizontalAlignment="Left" Margin="377,70,0,0" VerticalAlignment="Top" Click="R_Click"/>
  11. <RadioButton x:Name="G" Content="G" HorizontalAlignment="Left" Margin="377,98,0,0" VerticalAlignment="Top" Click="G_Click"/>
  12. <RadioButton x:Name="B" Content="B" HorizontalAlignment="Left" Margin="377,130,0,0" VerticalAlignment="Top" Click="B_Click"/>
  13. </Grid>

 

  1. private void R_Click(object sender, RoutedEventArgs e)
  2. {
  3. var bursh = Resources["RectFill"];
  4. if (bursh is LinearGradientBrush)
  5. {
  6. LinearGradientBrush Ibursh = (LinearGradientBrush)bursh;
  7. Ibursh = new LinearGradientBrush()
  8. {
  9. GradientStops = new GradientStopCollection()
  10. {
  11. new GradientStop(Colors.BurlyWood,0),
  12. new GradientStop(Colors.Red,1)
  13. }
  14. };
  15. Resources["RectFill"] = Ibursh;
  16. }
  17. }
  18. private void G_Click(object sender, RoutedEventArgs e)
  19. {
  20. var bursh = Resources["RectFill"];
  21. if (bursh is LinearGradientBrush)
  22. {
  23. LinearGradientBrush Ibursh = (LinearGradientBrush)bursh;
  24. Ibursh = new LinearGradientBrush()
  25. {
  26. GradientStops = new GradientStopCollection()
  27. {
  28. new GradientStop(Colors.BurlyWood,0),
  29. new GradientStop(Colors.Green,1)
  30. }
  31. };
  32. Resources["RectFill"] = Ibursh;
  33. }
  34. }
  35. private void B_Click(object sender, RoutedEventArgs e)
  36. {
  37. var bursh = Resources["RectFill"];
  38. if (bursh is LinearGradientBrush)
  39. {
  40. LinearGradientBrush Ibursh = (LinearGradientBrush)bursh;
  41. Ibursh = new LinearGradientBrush()
  42. {
  43. GradientStops = new GradientStopCollection()
  44. {
  45. new GradientStop(Colors.BurlyWood,0),
  46. new GradientStop(Colors.Blue,1)
  47. }
  48. };
  49. Resources["RectFill"] = Ibursh;
  50. }
  51. }

 

 

WPF 精修篇 动态资源

上一篇:基于C# WPF框架的贪吃蛇


下一篇:2019-11-29-C#-性能分析-反射-VS-配置文件-VS-预编译