WPF窗口颜色变更方法
创始人
2024-06-18 11:21:05
0

WPF开发工具中有一种常用实现方法,就是窗口的操作。我们将会在这篇文章中为大家实现WPF窗口颜色的变更,希望对大家有所帮助。#t#

WPF窗口颜色目标:

动态变更窗口的底色(当然,可以扩展为其他元素的样式)

WPF窗口颜色变更思路:

创建两个资源文件(Resource Dictionary),一个用来存放默认样式(Default.xaml),一个用来存放其他样式(HotHot.xaml);

在需要变更样式的窗体中(本例中为:WinWords),使用动态样式(... Style="{DynamicResource styleBcakground}")

在Application类中(方便调用),添加一个应用样式的公共方法(ApplySkin)

在主窗体中(本例是在WinWords窗体中通过按钮点击事件)调用Application中应用样式方法(ApplySkin)

在本例中,WinWords窗体启动时,自动调用了ApplySkin方法来应用默认的样式(Default)

OK,WPF窗口颜色代码如下:

  1. < HOME_DIR>\Resources\Skins
    \Default.xaml  
  2. < !-- Background Style --> 
  3. < Style x:Key="styleBackground"> 
  4. < Setter Property="Control.Background"> 
  5. < Setter.Value> 
  6. < LinearGradientBrush StartPoint=
    "0,0.5" EndPoint="1,0.5" Opacity="0.5"> 
  7. < GradientStop Color="LightSkyBlue"
     Offset="0" /> 
  8. < GradientStop Color=
    "WhiteSmoke" Offset="0.5" /> 
  9. < GradientStop Color="Light
    SkyBlue" Offset="1" /> 
  10. < /LinearGradientBrush> 
  11. < /Setter.Value> 
  12. < /Setter> 
  13. < /Style> 
  14. < HOME_DIR>\Resources\Skins\HotHot.xaml  
  15. < !-- Background Style --> 
  16. < Style x:Key="styleBackground"> 
  17. < Setter Property="Control.Background"> 
  18. < Setter.Value> 
  19. < LinearGradientBrush StartPoint=
    "0.5,0" EndPoint="0.5,1"> 
  20. < GradientStop Color="#50000000" 
    Offset="0.5" /> 
  21. < GradientStop Color="#ff999999" 
    Offset="1" /> 
  22. < /LinearGradientBrush> 
  23. < /Setter.Value> 
  24. < /Setter> 
  25. < /Style> 
  26. < HOME_DIR>\WinWords.xaml  
  27. < Grid Style="{DynamicResource 
    styleBackground}"> 
  28. < HOME_DIR>\WinWords.xaml.cs  
  29. public WinWords()  
  30. {  
  31. InitializeComponent();  
  32. this.ApplySkin("Default");  
  33. }  
  34. private void ApplySkin(string 
    pstrDictPath)  
  35. {  
  36. string skinDictPath = @".
    \Resources\Skins\" + pstrDictPath 
    + @".xaml";  
  37. Uri skinDictUri = new Uri(skinDict
    Path, UriKind.Relative);  
  38. MyCcApp app = Application.Current 
    as MyCcApp;  
  39. app.ApplySkin(skinDictUri);  
  40. }  
  41. private void btnTestSkining_Click
    (object sender, RoutedEventArgs e)  
  42. {  
  43. this.ApplySkin("HotHot");  
  44. }  
  45. < HOME_DIR>\MyCcApp.xaml.cs  
  46. public void ApplySkin(Uri 
    skinDictionaryUri)  
  47. {  
  48. ResourceDictionary skinDict =
     Application.LoadComponent(skin
    DictionaryUri) as ResourceDictionary;  
  49. Collection< ResourceDictionary> 
    mergedDicts = base.Resources.
    MergedDictionaries;  
  50. if (mergedDicts.Count > 0)  
  51. {  
  52. mergedDicts.Clear();  
  53. }  
  54. mergedDicts.Add(skinDict);  

上面介绍的内容就是WPF窗口颜色的变更实现方法。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...