WPF:换肤基础(临摹贴)

临摹对象《 Creating a Skinned User Interface in WPF

目标:动态变更窗口的底色(当然,可以扩展为其他元素的样式)
思路:
  •   创建两个资源文件(Resource Dictionary),一个用来存放默认样式(Default.xaml),一个用来存放其他样式(HotHot.xaml);
  •   在需要变更样式的窗体中(本例中为:WinWords),使用动态样式(... Style="{DynamicResource styleBcakground}")
  •   在Application类中(方便调用),添加一个应用样式的公共方法(ApplySkin)
  •   在主窗体中(本例是在WinWords窗体中通过按钮点击事件)调用Application中应用样式方法(ApplySkin)
  •   在本例中,WinWords窗体启动时,自动调用了ApplySkin方法来应用默认的样式(Default)

OK,代码如下:
<HOME_DIR>\Resources\Skins\Default.xaml
 1       <!--  Background Style  -->
 2       < Style  x:Key ="styleBackground" >
 3           < Setter  Property ="Control.Background" >
 4               < Setter.Value >
 5                   < LinearGradientBrush  StartPoint ="0,0.5"  EndPoint ="1,0.5"  Opacity ="0.5" >
 6                       < GradientStop  Color ="LightSkyBlue"  Offset ="0"   />
 7                       < GradientStop  Color ="WhiteSmoke"  Offset ="0.5"   />
 8                       < GradientStop  Color ="LightSkyBlue"  Offset ="1"   />
 9                   </ LinearGradientBrush >
10               </ Setter.Value >
11           </ Setter >
12       </ Style >

<HOME_DIR>\Resources\Skins\HotHot.xaml
 1      <!--  Background Style  -->
 2      < Style  x:Key ="styleBackground" >
 3          < Setter  Property ="Control.Background" >
 4              < Setter.Value >
 5                  < LinearGradientBrush  StartPoint ="0.5,0"  EndPoint ="0.5,1" >
 6                      < GradientStop  Color ="#50000000"  Offset ="0.5"   />
 7                      < GradientStop  Color ="#ff999999"  Offset ="1"   />
 8                  </ LinearGradientBrush >
 9              </ Setter.Value >
10          </ Setter >
11      </ Style >

<HOME_DIR>\WinWords.xaml
1
2      < Grid  Style =" {DynamicResource styleBackground} " >
3

<HOME_DIR>\WinWords.xaml.cs
 1         public WinWords()
 2         {
 3             InitializeComponent();
 4             
 5             this.ApplySkin("Default");
 6         }
 7
 8         private void ApplySkin(string pstrDictPath)
 9         {
10             string skinDictPath = @".\Resources\Skins\" + pstrDictPath + @".xaml";
11             Uri skinDictUri = new Uri(skinDictPath, UriKind.Relative);
12
13             MyCcApp app = Application.Current as MyCcApp;
14             app.ApplySkin(skinDictUri);
15         }
16         private void btnTestSkining_Click(object sender, RoutedEventArgs e)
17        {
18             this.ApplySkin("HotHot");
19         }


<HOME_DIR>\MyCcApp.xaml.cs
 1         public void ApplySkin(Uri skinDictionaryUri)
 2         {
 3             ResourceDictionary skinDict = Application.LoadComponent(skinDictionaryUri) as ResourceDictionary;
 4
 5             Collection < ResourceDictionary >  mergedDicts = base.Resources.MergedDictionaries;
 6
 7             if (mergedDicts.Count > 0)
 8             {
 9                 mergedDicts.Clear();
10             }
11
12             mergedDicts.Add(skinDict);
13         }

哪里是关键大家自然一看就清楚了,查查MSDN,O了,^_^

你可能感兴趣的:(WPF)