WPF 皮肤

皮肤

  1. <!-- 在全局Application对象中指定样式(默认皮肤):app.xaml -->  
  2. <Application x:Class="WPF_Test.App"  
  3.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.              StartupUri="MainWindow.xaml">  
  6.     <Application.Resources>  
  7.         <Style x:Key="DialogStyle" TargetType="StackPanel">  
  8.             <Setter Property="Margin" Value="20"/>  
  9.         </Style>  
  10.         <Style x:Key="HeadingStyle" TargetType="Label">  
  11.             <Setter Property="FontSize" Value="10"/>  
  12.             <Setter Property="Foreground" Value="Blue"/>  
  13.         </Style>  
  14.         <Style x:Key="CancelButtonStyle" TargetType="Button">  
  15.         </Style>  
  16.     </Application.Resources>  
  17. </Application>  
  1. <!-- 皮肤样式:Skin1.xaml -->  
  2. <!-- 建议使用ResourceDictionary作为根 -->  
  3. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">  
  5.     <Style x:Key="DialogStyle" TargetType="StackPanel">  
  6.         <Setter Property="Margin" Value="20"/>  
  7.     </Style>  
  8.     <Style x:Key="HeadingStyle" TargetType="Label">  
  9.         <Setter Property="FontSize" Value="30"/>  
  10.         <Setter Property="Foreground" Value="BurlyWood"/>  
  11.     </Style>  
  12.     <Style x:Key="CancelButtonStyle" TargetType="Button">  
  13.         <Setter Property="Background" Value="Crimson"/>  
  14.     </Style>  
  15. </ResourceDictionary>  
  1. <!-- 使用皮肤,单页文件MySkin.xaml -->  
  2. <Page x:Class="WPF_Test.MySkin"  
  3.       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
  6.       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
  7.       mc:Ignorable="d"   
  8.       d:DesignHeight="300" d:DesignWidth="300"  
  9.     Title="MySkin">  
  10.   
  11.     <Grid>  
  12.         <!-- 动态绑定资源 -->  
  13.         <StackPanel Style="{DynamicResource DialogStyle}">  
  14.             <Label Style="{DynamicResource HeadingStyle}">Loading...</Label>  
  15.             <ProgressBar Value="35" MinHeight="20" Margin="20"/>  
  16.             <Button Style="{DynamicResource CancelButtonStyle}" Width="70">Cancel</Button>  
  17.         </StackPanel>  
  18.     </Grid>  
  19. </Page>  

  1. //主窗体MainWindow.xaml中指定皮肤   
  2. using System.IO;  
  3. using System.Windows.Markup;  
  4.   
  5. public MainWindow()  
  6. {  
  7.     #region 指定皮肤(不显示指定,则为默认皮肤)   
  8.     ResourceDictionary resources = null;  
  9.     using (FileStream fs = new FileStream(@"D:\Study\WPF\CodeTest\WPF_Test\WPF_Test\Skin\Skin1.xaml", FileMode.Open, FileAccess.Read))  
  10.     {  
  11.         resources = (ResourceDictionary)XamlReader.Load(fs);  
  12.     }  
  13.   
  14.     Application.Current.Resources = resources;  
  15.     #region   
  16.   
  17.     InitializeComponent();  
  18.   
  19.     //内嵌页面对象   
  20.     //...   
  21. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值