为了使WPF程序在不同版本的操作系统上保持一致的显示效果,我们需要重写WPF控件样式。这篇博客将展示如何创建一个Metro Style的WPF窗体。

首先先看一下最终窗体的效果图,

通过截图我们可以看出来这个窗体由两部分组成,顶部为最小化和关闭按钮,其他区域为窗体的显示区域。请看下面的具体实现代码,

MetroWindow样式:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MetroWindow.Resources.Styles">
<!--最小化按钮样式-->
<Style x:Key="WinMinBtnStyle" TargetType="Button">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Width" Value="25"/>
<Setter Property="Height" Value="25"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="MainBorder" Background="Transparent">
<Grid>
<ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="MainBorder" Property="Background" Value="#33a58d"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> <!--关闭按钮样式-->
<Style x:Key="WinCloseBtnStyle" TargetType="Button">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Width" Value="25"/>
<Setter Property="Height" Value="25"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="MainBorder" Background="Transparent">
<Grid>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="MainBorder" Property="Background" Value="#d44c45"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> <!--窗体控件模板-->
<ControlTemplate x:Key="MetroWindowTemplate" TargetType="{x:Type Window}">
<Border BorderBrush="#2a927c" BorderThickness="1" Background="White">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions> <Grid Grid.Row="0" Background="#2a927c">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions> <TextBlock x:Name="WindowTitleTbl" Grid.Column="0" Text="" FontFamily="Microsoft YaHei" VerticalAlignment="Center"
FontSize="12" FontWeight="Bold" Margin="10,0" Foreground="White"/> <Button x:Name="MinWinButton" Grid.Column="2" Style="{StaticResource WinMinBtnStyle}"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Center">
<Button.Content>
<StackPanel>
<Path Stroke="White" StrokeThickness="2" Data="M1,6 L18,6"/>
</StackPanel>
</Button.Content>
</Button> <Button x:Name="CloseWinButton" Grid.Column="3" Style="{StaticResource WinCloseBtnStyle}" Margin="2,0,8,0"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center">
<Button.Content>
<StackPanel>
<Path Stroke="White" StrokeThickness="2" Data="M2,2 L16,16 M2,16 L16,2"/>
</StackPanel>
</Button.Content>
</Button>
</Grid> <AdornerDecorator Grid.Row="1">
<ContentPresenter/>
</AdornerDecorator>
</Grid>
<Border.Effect>
<DropShadowEffect/>
</Border.Effect>
</Border>
</ControlTemplate> <Style x:Key="MetroWindowStyle" TargetType="{x:Type Window}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="Template" Value="{StaticResource MetroWindowTemplate}"/>
</Style>
</ResourceDictionary>

新建一个ModernWindow类,

C#

    public class ModernWindow : Window
{
private Button CloseButton;
private Button MinButton;
private TextBlock WindowTitleTbl; public ModernWindow()
{
this.Loaded += ModernWindow_Loaded;
} private void ModernWindow_Loaded(object sender, RoutedEventArgs e)
{
// 查找窗体模板
ControlTemplate metroWindowTemplate
= App.Current.Resources["MetroWindowTemplate"] as ControlTemplate; if (metroWindowTemplate != null)
{
CloseButton = metroWindowTemplate.FindName("CloseWinButton", this) as Button;
MinButton = metroWindowTemplate.FindName("MinWinButton", this) as Button; CloseButton.Click += CloseButton_Click;
MinButton.Click += MinButton_Click; WindowTitleTbl = metroWindowTemplate.FindName("WindowTitleTbl", this) as TextBlock;
}
} private void CloseButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
Close();
} private void MinButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.WindowState = System.Windows.WindowState.Minimized;
} /// <summary>
/// 实现窗体移动
/// </summary>
/// <param name="e"></param>
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
DragMove(); base.OnMouseLeftButtonDown(e);
}
}

现在我们就完成了Metro Style窗体了,现在对其进行应用。请看MainWindow实现:
XAML:

<local:ModernWindow x:Class="MetroWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MetroWindow"
Style="{StaticResource MetroWindowStyle}"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid> </Grid>
</local:ModernWindow>

C#:

    public partial class MainWindow : ModernWindow
{
public MainWindow()
{
InitializeComponent();
}
}

现在就完成了Metro Style窗体的制作。
自Win8发布以来,越来越多的桌面应用开始实现Metro样式。现在也有很多WPF MetroUI库,例如:http://mui.codeplex.com/。我们可以根据项目的实际情况选择现有的Metro UI/Control,当然也可以自己写。

代码请点击这里下载。

感谢您的阅读。

WPF 自定义Metro Style窗体的更多相关文章

  1. WPF:自定义Metro样式文件夹选择对话框FolderBrowserDialog

    1.前言 WPF并没有文件选择对话框,要用也就只有使用Winform版的控件.至今我也没有寻找到一个WPF版本的文件选择对话框. 可能是我眼浊,如果各位知道有功能比较健全的WPF版文件选择对话框.文件 ...

  2. WPF 自定义按钮 Style

    <Style TargetType="{x:Type Button}" x:Key="DefaultButton"> <Setter Prop ...

  3. [WPF]使用WindowChrome自定义Window Style

    1. 前言 做了WPF开发多年,一直未曾自己实现一个自定义Window Style,无论是<WPF编程宝典>或是各种博客都建议使用WindowStyle="None" ...

  4. [WPF自定义控件]使用WindowChrome自定义Window Style

    1. 为什么要自定义Window 对稍微有点规模的桌面软件来说自定义的Window几乎是标配了,一来设计师总是克制不住自己想想软件更个性化,为了UI的和谐修改Window也是必要的:二来多一行的空间可 ...

  5. [WPF自定义控件]?使用WindowChrome自定义Window Style

    原文:[WPF自定义控件]?使用WindowChrome自定义Window Style 1. 为什么要自定义Window 对稍微有点规模的桌面软件来说自定义的Window几乎是标配了,一来设计师总是克 ...

  6. WPF自定义Window样式(1)

    1. 引言 WPF是制作界面的一大利器.最近在做一个项目,用的就是WPF.既然使用了WPF了,那么理所当然的,需要自定义窗体样式.所使用的代码是在网上查到的,遗憾的是,整理完毕后,再找那篇帖子却怎么也 ...

  7. wpf 自定义圆形按钮

    wpf 自定义圆形按钮 效果图 默认样式 获取焦点样式 点击样式 下面是实现代码: 一个是自定义控件类,一个是控件类皮肤 using System; using System.Collections. ...

  8. WPF自定义窗口基类

    WPF自定义窗口基类时,窗口基类只定义.cs文件,xaml文件不定义.继承自定义窗口的类xaml文件的根节点就不再是<Window>,而是自定义窗口类名(若自定义窗口与继承者不在同一个命名 ...

  9. WPF 自定义 MessageBox (相对完善版)

    WPF 自定义 MessageBox (相对完善版)     基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当 ...

随机推荐

  1. C# XML和实体类之间相互转换(序列化和反序列化)

    我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. using System; using System.Collections.Ge ...

  2. MySQL一次插入多行数据

    CREATE TABLE `viewhistory` ( `viewid` ) NOT NULL AUTO_INCREMENT, `uid` ) NOT NULL, `video` ) NOT NUL ...

  3. RecyclerView notifyDataSetChanged不起作用

    一般listview设置完data后调用notifyDataSetChanged便可刷新布局界面,然而recycleview调用这个方法却没有任何反应.对于很多不熟悉recycleview的话很容易躺 ...

  4. ios oc 和 swfit 用dispatch_once 创建单例

    网上已经有方法了,我这里就是抄了下,原文链接 http://bj007.blog.51cto.com/1701577/649413 http://blog.csdn.net/u010124617/ar ...

  5. javascript 搜索并高亮显示

    2015年12月22日 15:45:08 星期二 情景: 用来筛选列表中的数据, 由于单条数据很简短, 没有用php+mysql去实现筛选功能, 只用javascript进行筛选, 匹配的高亮, 或者 ...

  6. 安装VisualSvn Server时遇到的问题

    安装标准版VisualSvnserver,端口443,启用https//,安装过程中报服务启动失败,后用命令行 msiexec /i VisualSVN-Server-2.7.3.msi NO_STA ...

  7. Maven实现直接部署Web项目到Tomcat7(转)

    转载自:http://my.oschina.net/angel243/blog/178554 以前在项目中很少使用Maven,最近自己学习了一下maven,真的是非常强大的项目构建工具,对于依赖包的定 ...

  8. 【python】getopt使用

    来源:http://blog.chinaunix.net/uid-21566578-id-438233.html 注意对比:[python]argparse模块 作者:limodou版权所有limod ...

  9. 【mysql】利用Navicat for MySQL的使用

    1. 查看sql语句 如果忘记了某个SQL语句怎么写,可以利用Navicat for MySQL的历史日志来查看 在Navicat for MySQL中,直接对数据库进行想要的操作,然后点击工具-&g ...

  10. osgconv 批量转换

    @echo offfor /f "delims=" %%i in ('dir/b *.osg') do ( "osgconv.exe" "%%~ni. ...