WPF窗口继承实现统一风格的自定义窗口
如何實現一個窗口的風格(style),讓所有的窗口都繼承這樣同樣的風格,包括標題欄,放大、縮小和關閉按鈕。
?那么,我們可不可以就建立一個Base窗口,然后將這個窗口的風格給設計好之后,所有的窗口都繼承自他呢?
答案是否定的,我們一定要知道,窗口是一個類,它可以繼承,但是風格(XAML)文件是繼承不了的。
所以我們能夠做到的是:
1、窗口類繼承,BaseWindow封裝窗口最大化按鈕,最小化按鈕的點擊等事件。
2、風格(Style)就利用屬性來設置,把window看成一個控件,利用Style=“BaseWindowStyle”就可以了。
以下是詳細的步驟:
1、新建一個BaseWindowStyle的模板文件。點擊項目名稱,右鍵“添加”->”資源字典”->輸入名稱為“BaseWindowStyle.xaml”,然后將以下的末班文件拷貝進去:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><ControlTemplate x:Key="WindowTemplateKey"TargetType="{x:Type Window}"><Border Background="{TemplateBinding Background}"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"><Grid><AdornerDecorator><ContentPresenter /></AdornerDecorator><ResizeGrip Visibility="Collapsed"IsTabStop="false"HorizontalAlignment="Right"x:Name="WindowResizeGrip"VerticalAlignment="Bottom" /></Grid></Border><ControlTemplate.Triggers><MultiTrigger><MultiTrigger.Conditions><Condition Property="ResizeMode"Value="CanResizeWithGrip" /><Condition Property="WindowState"Value="Normal" /></MultiTrigger.Conditions><Setter Property="Visibility"TargetName="WindowResizeGrip"Value="Visible" /></MultiTrigger></ControlTemplate.Triggers></ControlTemplate><ControlTemplate x:Key="BaseWindowControlTemplate" TargetType="{x:Type Window}"><DockPanel LastChildFill="True"><!--外邊框--><Border Width="Auto"Height="Auto"DockPanel.Dock="Top"Background="#FF7097D0"CornerRadius="0,0,0,0"x:Name="borderTitle"><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="*"></ColumnDefinition><ColumnDefinition Width="*"></ColumnDefinition></Grid.ColumnDefinitions><TextBlock Grid.Column="0" Margin="20,0,2,2" Name="Title" VerticalAlignment="Top" FontSize="20" Foreground="White" Text="{TemplateBinding Title}"/><StackPanel Grid.Column="1" HorizontalAlignment="Right" Orientation="Horizontal"><!--最小化按鈕--><Button x:Name="btnMin" Content="M" Margin="2,2,2,2" Style="{DynamicResource MinButtonStyle}"/><!--最大化按鈕--><Button x:Name="btnMax" Content="M" Margin="2,2,2,2" Style="{DynamicResource MaxButtonStyle}"/><!--關閉按鈕--><Button x:Name="btnClose" Content="M" Margin="2,2,2,2" Style="{DynamicResource CloseButtonStyle}"/></StackPanel></Grid></Border><Border Background="{TemplateBinding Background}"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"Width="Auto"Height="Auto"DockPanel.Dock="Top"CornerRadius="0,0,4,4"><AdornerDecorator><ContentPresenter /></AdornerDecorator></Border></DockPanel></ControlTemplate><Style x:Key="BaseWindowStyle"TargetType="{x:Type Window}"><Setter Property="Foreground"Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" /><Setter Property="Background"Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" /><Setter Property="Template" Value="{StaticResource BaseWindowControlTemplate}"/><Setter Property="AllowsTransparency"Value="True" /><Setter Property="WindowStyle"Value="None" /><Setter Property="BorderBrush"Value="#FF7097D0" /><Setter Property="BorderThickness"Value="4,4,4,4" /><Style.Triggers><Trigger Property="ResizeMode"Value="CanResizeWithGrip"><Setter Property="Template"Value="{StaticResource WindowTemplateKey}" /></Trigger></Style.Triggers></Style><!--最小化按鈕--><Style x:Key="MinButtonStyle" TargetType="{x:Type Button}"><Setter Property="Foreground" Value="Black"/><!--修改模板屬性--><Setter Property="Template"><Setter.Value><!--控件模板--><ControlTemplate TargetType="Button"><!--背景色--><Border x:Name="back" Opacity="0.8" CornerRadius="0" BorderBrush="#FFCDA05F" Background="#FFFFE9C9" BorderThickness="1" Margin="1" Padding="2,0,0,0"><!--按鈕內容--><Path x:Name="cp" Width="12" Height="12" Stroke="#FFCEA15F" StrokeThickness="3" Fill="Black"><Path.Data><PathGeometry Figures="M 0,6 H 6,6 " /></Path.Data></Path></Border><ControlTemplate.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" TargetName="back" Value="#FFD4BD9B"/></Trigger><Trigger Property="IsPressed" Value="True"><Setter Property="Background" TargetName="back" Value="#FFCDA05F"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style><!--最大化按鈕--><Style x:Key="MaxButtonStyle" TargetType="{x:Type Button}"><Setter Property="Foreground" Value="Black"/><!--修改模板屬性--><Setter Property="Template"><Setter.Value><!--控件模板--><ControlTemplate TargetType="Button"><!--背景色--><Border x:Name="back" Opacity="0.8" CornerRadius="0" BorderBrush="#FFCDA05F" Background="#FFFFE9C9" BorderThickness="1" Margin="1" Padding="2,0,0,0"><!--按鈕內容--><Path x:Name="cp" Width="12" Height="12" Stroke="#FFCEA15F" StrokeThickness="3" ><Path.Data><PathGeometry Figures="M 0,0 L 0,12 12,12 12,0 0,0" /></Path.Data></Path></Border><ControlTemplate.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" TargetName="back" Value="#FFD4BD9B"/></Trigger><Trigger Property="IsPressed" Value="True"><Setter Property="Background" TargetName="back" Value="#FFCDA05F"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style><!--關閉按鈕--><Style x:Key="CloseButtonStyle" TargetType="{x:Type Button}"><Setter Property="Foreground" Value="Black"/><!--修改模板屬性--><Setter Property="Template"><Setter.Value><!--控件模板--><ControlTemplate TargetType="Button"><!--背景色--><Border x:Name="back" Opacity="0.8" CornerRadius="0" BorderBrush="#FFCDA05F" Background="#FFFFE9C9" BorderThickness="1" Margin="1" Padding="2,0,0,0"><!--按鈕內容--><Path x:Name="cp" Width="12" Height="12" Stroke="#FFCEA15F" StrokeThickness="3" ><Path.Data><PathGeometry Figures="M 0,0 L 12,12 M 0,12 L 12,0" /></Path.Data></Path></Border><ControlTemplate.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" TargetName="back" Value="#FFD4BD9B"/></Trigger><Trigger Property="IsPressed" Value="True"><Setter Property="Background" TargetName="back" Value="#FFCDA05F"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></ResourceDictionary>?
2、引用該模板文件。資源文件建立后,我們需要在程序中引用這個文件,打開APP.XAML,將引用的BaseWindowStyle添加進去:
<Applicationxmlns="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" mc:Ignorable="d" x:Class="MyFirst.App"StartupUri="MainWindow.xaml"><Application.Resources><ResourceDictionary Source="BaseWindowStyle.xaml"></ResourceDictionary></Application.Resources> </Application>3、建立BaseWindow類,實現窗口點擊,拖動等事件。在工程上點擊右鍵“添加”->“類”->輸入”BaseWindow.cs“
該類繼承自Window,然后要載入模板文件,在模板文件中獲取最小化、最大化等按鈕,然后將這些按鈕的點擊事件和窗口綁定起來,實現窗口的響應:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Input;namespace MyFirst {public class BaseWindow : Window{public BaseWindow(){InitializeStyle();this.Loaded += delegate{InitializeEvent();};}private void InitializeEvent(){ControlTemplate baseWindowTemplate = (ControlTemplate)App.Current.Resources["BaseWindowControlTemplate"];Button minBtn = (Button)baseWindowTemplate.FindName("btnMin", this);minBtn.Click += delegate{this.WindowState = WindowState.Minimized;};Button maxBtn = (Button)baseWindowTemplate.FindName("btnMax", this);maxBtn.Click += delegate{this.WindowState = (this.WindowState == WindowState.Normal ? WindowState.Maximized : WindowState.Normal);};Button closeBtn = (Button)baseWindowTemplate.FindName("btnClose", this);closeBtn.Click += delegate{this.Close();};Border borderTitle = (Border)baseWindowTemplate.FindName("borderTitle", this);borderTitle.MouseMove += delegate(object sender, MouseEventArgs e){if (e.LeftButton == MouseButtonState.Pressed){this.DragMove();}};borderTitle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e){if (e.ClickCount >= 2){maxBtn.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));}};}private void InitializeStyle(){this.Style = (Style)App.Current.Resources["BaseWindowStyle"];}} }?
4,其它窗口類繼承自BaseWindow:
public partial class?MainWindow : BaseWindow
5,其它窗口的風格指向BaseWindowStyle。
這里要注意一點Winodow的風格可不是直接 Style=”BaseWindowStyle” 那么簡單,可以看看MainWindow.xaml的代碼:
<local:BaseWindow x:Class="MyFirst.Login"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:src="clr-namespace:MyFirst"Title="MainWindow" Height="300" Width="300"></local:BaseWindow>?
經過這么多步,我們終于實現了凡是繼承自BaseWindow的窗口,都有統一的風格了!
當然,如上的窗口還不是那么漂亮完美,但是相信你有辦法把它做的美美的!
轉載于:https://www.cnblogs.com/DreamRecorder/p/9662081.html
總結
以上是生活随笔為你收集整理的WPF窗口继承实现统一风格的自定义窗口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Tornado入门三
- 下一篇: Android 高效的`InjectVi