自定义窗体,简简单单实现

style文件xmal:

<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 x:Name="WindowResizeGrip" Visibility="Collapsed" IsTabStop="false" HorizontalAlignment="Right" 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 x:Name="borderTitle" DockPanel.Dock="Top" Height="30" BorderBrush="#FFA9A9A9" BorderThickness="0,0,2,0">

                <Border BorderBrush="#FF494949" BorderThickness="1,1,1,0">

                    <Border.Background>

                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                            <GradientStop Color="#FFF4F4F4" Offset="0"/>

                            <GradientStop Color="#FFEBEBEB" Offset="1"/>

                        </LinearGradientBrush>

                    </Border.Background>

                    <Grid>

                        <TextBlock x:Name="Title" VerticalAlignment="Center" Margin="20,0,0,0"></TextBlock>

                        <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">

                            <!--最小化按钮-->

                            <Button x:Name="btnMin" Margin="2,4,0,4" Height="20" Width="30" Style="{DynamicResource HomeBtnStyle}">

                                <Image Source="/WisHotel;component/Images/HomePageImages/最小化图标.png" Height="18" Width="18" SnapsToDevicePixels="True"/>

                            </Button>

                            <!--最大化按钮-->

                            <Button x:Name="btnMax" Margin="2,4,0,4" Height="20" Width="30" Style="{DynamicResource HomeBtnStyle}">

                                <Image Source="/WisHotel;component/Images/HomePageImages/窗口图标.png" Height="18" Width="18"/>

                            </Button>

                            <!--关闭按钮-->

                            <Button x:Name="btnClose" Margin="2,4,10,4" Height="20" Width="30" Style="{DynamicResource HomeBtnStyle}">

                                <Image Source="/WisHotel;component/Images/HomePageImages/关闭图标.png" Height="18" Width="18"/>

                            </Button>

                        </StackPanel>

                    </Grid>

                </Border>

            </Border>

            <Border BorderBrush="#FFA9A9A9" BorderThickness="0,0,2,2">

                <Border BorderBrush="#FF494949" Background="#FFEBEBEB" BorderThickness="1,0,1,1">

                    <Border BorderBrush="#FFEBEBEB" BorderThickness="2,1">

                        <Border BorderBrush="#FF494949" BorderThickness="1">

                            <Border Background="White">

                                <AdornerDecorator>

                                    <ContentPresenter />

                                </AdornerDecorator>

                            </Border>

                        </Border>

                    </Border>

                </Border>

            </Border>

        </DockPanel>

    </ControlTemplate>

    <Style x:Key="BaseWindowStyle" TargetType="{x:Type Window}">

        <Setter Property="Template" Value="{StaticResource BaseWindowControlTemplate}"/>

        <Setter Property="AllowsTransparency" Value="True" />

        <Setter Property="WindowStyle" Value="None" />

        <Style.Triggers>

            <Trigger Property="ResizeMode" Value="CanResizeWithGrip">

                <Setter Property="Template" Value="{StaticResource WindowTemplateKey}" />

            </Trigger>

        </Style.Triggers>

    </Style>

</ResourceDictionary>

继承类cs:

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;



namespace WisHotel.Common

{

    public class BaseWindow : Window

    {

        public BaseWindow()

        {

            //居中显示

            WindowStartupLocation = WindowStartupLocation.CenterScreen;

            //初始化样式

            this.Style = (Style)App.Current.Resources["BaseWindowStyle"];
       //下面两句是做的这个基类是用来做类似弹出编辑的小窗体和MessageBox
this.ShowInTaskbar = false;//不在任务栏显示 this.Owner = Application.Current.MainWindow;//绑定主窗口
this.Loaded += delegate { InitializeEvent(); }; } private void InitializeEvent() { ControlTemplate baseWindowTemplate = (ControlTemplate)App.Current.Resources["BaseWindowControlTemplate"]; TextBlock TitleTextBlock = (TextBlock)baseWindowTemplate.FindName("Title", this); TitleTextBlock.Text = this.Title; 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));//双击放大 } }; } } }

 要使用这个自定义窗体,继承即可:

<src:BaseWindow x:Class="xxx.yyy"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:src="clr-namespace:xxx"

             Width="450" Height="480">

<Grid/>

</src:BaseWindow>

 

你可能感兴趣的:(自定义)