WPF 皮肤

皮肤

<!-- 在全局Application对象中指定样式(默认皮肤):app.xaml -->
<Application x:Class="WPF_Test.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style x:Key="DialogStyle" TargetType="StackPanel">
            <Setter Property="Margin" Value="20"/>
        </Style>
        <Style x:Key="HeadingStyle" TargetType="Label">
            <Setter Property="FontSize" Value="10"/>
            <Setter Property="Foreground" Value="Blue"/>
        </Style>
        <Style x:Key="CancelButtonStyle" TargetType="Button">
        </Style>
    </Application.Resources>
</Application>
<!-- 皮肤样式:Skin1.xaml -->
<!-- 建议使用ResourceDictionary作为根 -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="DialogStyle" TargetType="StackPanel">
        <Setter Property="Margin" Value="20"/>
    </Style>
    <Style x:Key="HeadingStyle" TargetType="Label">
        <Setter Property="FontSize" Value="30"/>
        <Setter Property="Foreground" Value="BurlyWood"/>
    </Style>
    <Style x:Key="CancelButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="Crimson"/>
    </Style>
</ResourceDictionary>
<!-- 使用皮肤,单页文件MySkin.xaml -->
<Page x:Class="WPF_Test.MySkin"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
	Title="MySkin">

    <Grid>
        <!-- 动态绑定资源 -->
        <StackPanel Style="{DynamicResource DialogStyle}">
            <Label Style="{DynamicResource HeadingStyle}">Loading...</Label>
            <ProgressBar Value="35" MinHeight="20" Margin="20"/>
            <Button Style="{DynamicResource CancelButtonStyle}" Width="70">Cancel</Button>
        </StackPanel>
    </Grid>
</Page>

//主窗体MainWindow.xaml中指定皮肤
using System.IO;
using System.Windows.Markup;

public MainWindow()
{
    #region 指定皮肤(不显示指定,则为默认皮肤)
    ResourceDictionary resources = null;
    using (FileStream fs = new FileStream(@"D:\Study\WPF\CodeTest\WPF_Test\WPF_Test\Skin\Skin1.xaml", FileMode.Open, FileAccess.Read))
    {
        resources = (ResourceDictionary)XamlReader.Load(fs);
    }

    Application.Current.Resources = resources;
    #region

    InitializeComponent();

    //内嵌页面对象
    //...
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值