WPF 精修篇 动态资源

原文:WPF 精修篇 动态资源

动态资源 使用 DynamicResource 关键字

静态 就是 StaticResource

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

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

效果


  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. }

原文地址:https://www.cnblogs.com/lonelyxmas/p/12075517.html

时间: 2024-08-30 03:13:50

WPF 精修篇 动态资源的相关文章

WPF 精修篇 静态资源

原文:WPF 精修篇 静态资源 在WPF中 如果设置好了一个控件样式或者矩形样式 如果Copy出一个新的 那么样式也会双份 比如 下面的矩形 我定义好了一个 Copy 以后 就出现一个新的 但是改变样式就会要改变俩次 很麻烦 解决方案  把共同样式 设置成静态资源 <Window.Resources> <LinearGradientBrush x:Key="RectFill" EndPoint="0.5,1" StartPoint="0.

WPF 精修篇 管理资源字典

原文:WPF 精修篇 管理资源字典 样式太多  每个界面可能需要全局的样式 有没有肯能 WPF 中的样式 像Asp.net中 的CSS一样管理那 有的 有资源字典 BurshDictionary <LinearGradientBrush x:Key="RectFill" EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="BurlyWood" O

WPF 精修篇 依赖属性

原文:WPF 精修篇 依赖属性 依赖属性使用场景 1. 希望可在样式中设置属性. 2. 希望属性支持数据绑定. 3. 希望可使用动态资源引用设置属性. 4. 希望从元素树中的父元素自动继承属性值. 5. 希望属性可进行动画处理. 6. 希望属性系统在属性系统.环境或用户执行的操作或者读取并使用样式更改了属性以前的值时报告. 7. 希望使用已建立的.WPF 进程也使用的元数据约定,例如报告更改属性值时是否要求布局系统重新编写元素的可视化对象. 依赖属性生成 PropertyMetadata 后面可

WPF 精修篇 动画组TransformGroup

原文:WPF 精修篇 动画组TransformGroup 动画分组 TransformGroup 一个元素可能要有缩放 ScaleTransform和移动 TranslateTransform等多个效果组合 就需要分组 RenderTransformOrigin 中心点设置 "0.5,0.5" 为中间 在编辑器中  有设置 <Grid> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Widt

WPF 精修篇 拖拽 DragDrop

原文:WPF 精修篇 拖拽 DragDrop WPF 实现拖拽 效果 <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="197*"/> <ColumnDefinition Width="209*"/> <ColumnDefinition Width="111*"/> </Grid.ColumnDefinitions&g

WPF 精修篇 数据触发器

原文:WPF 精修篇 数据触发器 数据触发器 可以使用Binding 来绑定控件 或者数据源 来触发相关动作 举栗子 <Window.Resources> <Style TargetType="{x:Type Label}"> <Style.Triggers> <DataTrigger Binding="{Binding ElementName=red,Path=IsChecked}" Value="True&qu

WPF 精修篇 倾斜 SkewTransform

原文:WPF 精修篇 倾斜 SkewTransform 倾斜 SkewTransform AngleX 倾斜X角度 AngleY 倾斜Y角度 CenterX CenterY 中心点 <StackPanel Orientation="Horizontal"> <Image Source="Image/Dog.jpg" Width="200" Height="200"> <Image.RenderTr

WPF 精修篇 数据绑定到对象

原文:WPF 精修篇 数据绑定到对象 数据绑定到对象 首先 我们需要一个对象 public class Preson { private string name; public string Name { get { return name; } set { name = value; } } private string address; public string Address { get { return address; } set { address = value; } } pri

WPF 精修篇 长时间线程加取消功能

原文:WPF 精修篇 长时间线程加取消功能 <Grid> <Grid.RowDefinitions> <RowDefinition Height="11*"/> <RowDefinition Height="29*"/> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal" Margin="0"