為了使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,當然也可以自己寫。
代碼請點擊這里下載。
感謝您的閱讀。