皮肤
- <!-- 在全局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();
- //内嵌页面对象
- //...
- }