基于WPF封裝一個(gè)可擴(kuò)展的Window
前言
WPF中Window相信大家都很熟悉,有時(shí)我們有一些自定義需求默認(rèn)Window是無(wú)法滿足的,比如在標(biāo)題欄上放一些自己東西,這個(gè)時(shí)候我們就需要寫(xiě)一個(gè)自己的Window,實(shí)現(xiàn)起來(lái)也很簡(jiǎn)單,只要給Window設(shè)置一個(gè)WindowChrome.WindowChrome附加屬性就可以實(shí)現(xiàn),WindowChrome 可以讓你自定義窗口的非工作區(qū)的外觀和行為。非工作區(qū)就是窗口的標(biāo)題欄和邊框,通常由操作系統(tǒng)繪制和管理。WindowChrome 可以讓你將 WPF 的內(nèi)容擴(kuò)展到非工作區(qū),同時(shí)保留一些系統(tǒng)的功能和行為,比如調(diào)整大小,移動(dòng),最大化,最小化等。
一、示例代碼
1.1 基本使用
<local:CustomWindow
x:Class="CustomWindowDemo.Window1"
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:local="clr-namespace:CustomWindowDemo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="Window1"
Width="800"
Height="450"
Icon="/logo.png"
mc:Ignorable="d">
<Grid />
</local:CustomWindow>
1.2 自定義標(biāo)題欄高度
<local:CustomWindow
x:Class="CustomWindowDemo.Window1"
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:local="clr-namespace:CustomWindowDemo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="Window1"
Width="800"
Height="450"
CaptionHeight="35"
Icon="/logo.png"
mc:Ignorable="d">
<Grid />
</local:CustomWindow>
1.3 自定義標(biāo)題欄顏色
<local:CustomWindow
x:Class="CustomWindowDemo.Window1"
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:local="clr-namespace:CustomWindowDemo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="Window1"
Width="800"
Height="450"
CaptionBackground="Blue"
Icon="/logo.png"
mc:Ignorable="d">
<Grid />
</local:CustomWindow>
1.4 自定義標(biāo)題欄內(nèi)容
<local:CustomWindow
x:Class="CustomWindowDemo.Window1"
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:local="clr-namespace:CustomWindowDemo"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="Window1"
Width="800"
Height="450"
Icon="/logo.png"
mc:Ignorable="d">
<local:CustomWindow.CaptionBarContent>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Button
Margin="5"
Padding="2"
VerticalAlignment="Center"
Background="Transparent"
BorderThickness="0"
WindowChrome.IsHitTestVisibleInChrome="True">
<StackPanel Orientation="Horizontal">
<Polygon
VerticalAlignment="Center"
Fill="White"
Points="0,6 6,0 6,12" />
<TextBlock
Margin="4,0,0,0"
VerticalAlignment="Center"
FontSize="14"
Foreground="White"
Text="返回" />
</StackPanel>
</Button>
<TextBlock
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="14"
Foreground="White"
Text="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Title}" />
<Button
Grid.Column="2"
Margin="5"
Padding="2"
VerticalAlignment="Center"
Background="Transparent"
BorderThickness="0"
FontSize="14"
Foreground="White"
WindowChrome.IsHitTestVisibleInChrome="True">
<StackPanel Orientation="Horizontal">
<TextBlock
Margin="0,0,4,0"
VerticalAlignment="Center"
Text="Admin" />
<Polyline
VerticalAlignment="Center"
Points="0,0 5,5 10,0"
Stroke="White"
StrokeThickness="2" />
</StackPanel>
</Button>
</Grid>
</local:CustomWindow.CaptionBarContent>
<Grid />
</local:CustomWindow>
二、綜合案例

到此這篇關(guān)于基于WPF封裝一個(gè)可擴(kuò)展的Window的文章就介紹到這了,更多相關(guān)WPF封裝可擴(kuò)展Window內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一個(gè)可攜帶附加消息的增強(qiáng)消息框MessageBoxEx
一個(gè)可攜帶附加消息的增強(qiáng)消息框MessageBoxEx分享給大家,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
C# 使用鼠標(biāo)點(diǎn)擊對(duì)Chart控件實(shí)現(xiàn)數(shù)據(jù)提示效果
這篇文章主要介紹了C# 使用鼠標(biāo)點(diǎn)擊對(duì)Chart控件實(shí)現(xiàn)數(shù)據(jù)提示效果,文章給予上一篇的詳細(xì)內(nèi)容做延伸介紹,需要的小伙伴可任意參考一下2022-08-08
如何用C#找出數(shù)組中只出現(xiàn)了一次的數(shù)字
數(shù)組從字面上理解就是存放一組數(shù),下面這篇文章主要給大家介紹了關(guān)于如何用C#找出數(shù)組中只出現(xiàn)了一次的數(shù)字,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
C# 10分鐘完成百度人臉識(shí)別(入門(mén)篇)
這篇文章主要介紹了C# 10分鐘完成百度人臉識(shí)別(入門(mén)篇),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
DevExpress實(shí)現(xiàn)為T(mén)extEdit設(shè)置水印文字的方法
這篇文章主要介紹了DevExpress實(shí)現(xiàn)為T(mén)extEdit設(shè)置水印文字的方法,對(duì)C#程序設(shè)計(jì)人員來(lái)說(shuō)是一個(gè)很實(shí)用的技巧,需要的朋友可以參考下2014-08-08

