WPF自定义Window窗体样式

简介: 原文:WPF自定义Window窗体样式资源文件代码: ...
原文: WPF自定义Window窗体样式

资源文件代码:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!-- 窗体按钮模板 -->
    <ControlTemplate x:Key="tmplWindowBtn" TargetType="{x:Type Button}">
        <Border x:Name="bd" Width="28" Height="18" Background="Transparent"  >
            <Grid>
                <Image x:Name="img" Stretch="Fill" Source="{TemplateBinding Tag}"  />
                <Border x:Name="bdMask" Opacity="0.3" Visibility="Collapsed" Background="#001122" Margin="1 0 1 1" CornerRadius="0 0 3 3"></Border>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter TargetName="bdMask" Property="Visibility" Value="Visible"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="true">
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    <!-- 窗体模板 -->
    <ControlTemplate x:Key="tmplWindowEx" TargetType="{x:Type Window}">
        <Border>
            <Border CornerRadius="5" Background="#0998B8" Margin="{Binding BorderMargin}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="26"></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <TextBlock Text="{TemplateBinding Title}" Margin="10 0 0 0" FontFamily="微软雅黑,黑体" FontSize="12" Foreground="#fff" VerticalAlignment="Center"></TextBlock>
                    <StackPanel Orientation="Horizontal" Background="#0998B8" HorizontalAlignment="{Binding BtnPanelHorizontalAlignment}" Width="100" Margin="88 0 0 0">
                        <StackPanel Orientation="Horizontal" Margin="10 0 5 0" VerticalAlignment="Top" HorizontalAlignment="Right">
                            <Button x:Name="btnMinimize" Template="{StaticResource tmplWindowBtn}" WindowChrome.IsHitTestVisibleInChrome="True" Command="{Binding DataContext.WindowBtnCommand, RelativeSource={RelativeSource AncestorType=Window}}" CommandParameter="1" Visibility="{Binding BtnMinimizeVisibility}" >
                                <Button.Tag>
                                    <BitmapImage UriSource="/SunCreate.Common.Controls;Component/WindowEx/Images/btnWindowMin.png"/>
                                </Button.Tag>
                            </Button>
                            <Button x:Name="btnMaximize" Template="{StaticResource tmplWindowBtn}" WindowChrome.IsHitTestVisibleInChrome="True" Command="{Binding DataContext.WindowBtnCommand, RelativeSource={RelativeSource AncestorType=Window}}" CommandParameter="2" Visibility="{Binding BtnMaximizeVisibility}" >
                                <Button.Tag>
                                    <BitmapImage UriSource="/SunCreate.Common.Controls;Component/WindowEx/Images/btnWindowMax.png"/>
                                </Button.Tag>
                            </Button>
                            <Button x:Name="btnClose" Template="{StaticResource tmplWindowBtn}" WindowChrome.IsHitTestVisibleInChrome="True" Command="{Binding DataContext.WindowBtnCommand, RelativeSource={RelativeSource AncestorType=Window}}" CommandParameter="3">
                                <Button.Tag>
                                    <BitmapImage UriSource="/SunCreate.Common.Controls;Component/WindowEx/Images/btnWindowClose.png"/>
                                </Button.Tag>
                            </Button>
                        </StackPanel>
                    </StackPanel>
                    <Border Background="#d6e7f1" CornerRadius="3 0 3 3" Grid.Row="1" Margin="3" >
                        <ContentPresenter ></ContentPresenter>
                    </Border>
                </Grid>
            </Border>
        </Border>
    </ControlTemplate>
    <!-- 窗体样式 -->
    <Style x:Key="stlWindowEx" TargetType="{x:Type Window}">
        <Setter Property="Template" Value="{StaticResource tmplWindowEx}"/>
        <Setter Property="AllowsTransparency" Value="True"></Setter>
        <Setter Property="Background" Value="Transparent"></Setter>
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="ResizeMode" Value="NoResize" />
        <Setter Property="WindowStyle" Value="None" />
        <Setter Property="WindowChrome.WindowChrome">
            <Setter.Value>
                <WindowChrome CornerRadius="5 5 0 0"
                              CaptionHeight="35"
                              GlassFrameThickness="0"
                              UseAeroCaptionButtons="False"
                              NonClientFrameEdges="None">
                </WindowChrome>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
    
View Code

自定义窗体封装WindowEx类代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Resources;

namespace SunCreate.Common.Controls
{
    /// <summary>
    /// 窗体封装
    /// </summary>
    public class WindowEx : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private ICommand _WindowBtnCommand;
        /// <summary>
        /// 窗体按钮命令
        /// </summary>
        public ICommand WindowBtnCommand
        {
            get
            {
                return _WindowBtnCommand;
            }
            set
            {
                _WindowBtnCommand = value;
                OnPropertyChanged("WindowBtnCommand");
            }
        }

        private Thickness _BorderMargin = new Thickness(0, 0, 0, 0);
        public Thickness BorderMargin
        {
            get
            {
                return _BorderMargin;
            }
            set
            {
                _BorderMargin = value;
                OnPropertyChanged("BorderMargin");
            }
        }

        private HorizontalAlignment _BtnPanelHorizontalAlignment = HorizontalAlignment.Right;
        /// <summary>
        /// 窗体按钮的Panel位置
        /// </summary>
        public HorizontalAlignment BtnPanelHorizontalAlignment
        {
            get
            {
                return _BtnPanelHorizontalAlignment;
            }
            set
            {
                _BtnPanelHorizontalAlignment = value;
                OnPropertyChanged("BtnPanelHorizontalAlignment");
            }
        }

        private Visibility _BtnMinimizeVisibility = Visibility.Visible;
        /// <summary>
        /// 窗体最小化按钮的显示状态
        /// </summary>
        public Visibility BtnMinimizeVisibility
        {
            get
            {
                return _BtnMinimizeVisibility;
            }
            set
            {
                _BtnMinimizeVisibility = value;
                OnPropertyChanged("BtnMinimizeVisibility");
            }
        }

        private Visibility _BtnMaximizeVisibility = Visibility.Visible;
        /// <summary>
        /// 窗体最大化按钮的显示状态
        /// </summary>
        public Visibility BtnMaximizeVisibility
        {
            get
            {
                return _BtnMaximizeVisibility;
            }
            set
            {
                _BtnMaximizeVisibility = value;
                OnPropertyChanged("BtnMaximizeVisibility");
            }
        }

        /// <summary>
        /// 窗体 构造函数
        /// </summary>
        public WindowEx()
        {
            this.DataContext = this;
            this.ShowInTaskbar = false;

            #region 窗体样式设置
            Uri uri = new Uri("/SunCreate.Common.Controls;Component/WindowEx/WindowExResource.xaml", UriKind.Relative);
            ResourceDictionary rd = new ResourceDictionary();
            rd.Source = uri;
            this.Style = rd["stlWindowEx"] as Style;
            #endregion

            #region 窗体按钮事件
            WindowBtnCommand windowBtnCommand = new WindowBtnCommand();
            windowBtnCommand.DoAction = (parameter) =>
            {
                if (parameter == 1) //最小化
                {
                    this.BorderMargin = new Thickness(1, 0, 0, 0);
                    BtnPanelHorizontalAlignment = HorizontalAlignment.Left;
                    BtnMinimizeVisibility = Visibility.Collapsed;
                    this.WindowState = WindowState.Minimized;
                }
                if (parameter == 2) //窗口还原、最大化
                {
                    if (this.WindowState == WindowState.Normal)
                    {
                        double taskBarHeight = SystemParameters.PrimaryScreenHeight - SystemParameters.WorkArea.Height;
                        double taskBarWidth = SystemParameters.PrimaryScreenWidth - SystemParameters.WorkArea.Width;
                        if (taskBarWidth > 0)
                        {
                            this.BorderMargin = new Thickness(0, 0, taskBarWidth, 0);
                        }
                        if (taskBarHeight > 0)
                        {
                            this.BorderMargin = new Thickness(0, 0, 0, taskBarHeight);
                        }
                        BtnPanelHorizontalAlignment = HorizontalAlignment.Right;
                        BtnMinimizeVisibility = Visibility.Visible;
                        this.WindowState = WindowState.Maximized;
                    }
                    else if (this.WindowState == WindowState.Maximized)
                    {
                        this.BorderMargin = new Thickness(0, 0, 0, 0);
                        BtnPanelHorizontalAlignment = HorizontalAlignment.Right;
                        BtnMinimizeVisibility = Visibility.Visible;
                        this.WindowState = WindowState.Normal;
                    }
                    else if (this.WindowState == WindowState.Minimized)
                    {
                        this.BorderMargin = new Thickness(0, 0, 0, 0);
                        BtnPanelHorizontalAlignment = HorizontalAlignment.Right;
                        BtnMinimizeVisibility = Visibility.Visible;
                        this.WindowState = WindowState.Normal;
                    }
                }
                if (parameter == 3) //关闭窗口
                {
                    this.Close();
                }
            };
            this.WindowBtnCommand = windowBtnCommand;
            this.StateChanged += (s, e) =>
            {
                if (this.WindowState == WindowState.Maximized)
                {
                    BtnPanelHorizontalAlignment = HorizontalAlignment.Right;
                    BtnMinimizeVisibility = Visibility.Visible;
                    double taskBarHeight = SystemParameters.PrimaryScreenHeight - SystemParameters.WorkArea.Height;
                    double taskBarWidth = SystemParameters.PrimaryScreenWidth - SystemParameters.WorkArea.Width;
                    if (taskBarWidth > 0)
                    {
                        this.BorderMargin = new Thickness(0, 0, taskBarWidth, 0);
                    }
                    if (taskBarHeight > 0)
                    {
                        this.BorderMargin = new Thickness(0, 0, 0, taskBarHeight);
                    }
                }
                if (this.WindowState == WindowState.Normal)
                {
                    this.BorderMargin = new Thickness(0, 0, 0, 0);
                    BtnPanelHorizontalAlignment = HorizontalAlignment.Right;
                    BtnMinimizeVisibility = Visibility.Visible;
                }
            };
            #endregion

        }

        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}
View Code

窗体最小化、最大化、关闭按钮的命令WindowBtnCommand:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SunCreate.Common.Controls
{
    public class WindowBtnCommand : ICommand
    {
        public Action<int> DoAction { get; set; }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            if (DoAction != null)
            {
                DoAction(Convert.ToInt32(parameter));
            }
        }
    }
}
View Code

使用WindowEx类的示例代码:

<ui:WindowEx x:Class="SunCreate.Common.Controls.Demo.MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ui="clr-namespace:SunCreate.Common.Controls;assembly=SunCreate.Common.Controls"
        Title="视频播放视频播放ABCDEFG" Height="300" Width="500" WindowStartupLocation="CenterScreen"
        BtnMinimizeVisibility="Visible" BtnMaximizeVisibility="Visible" >
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/SunCreate.Common.Controls;Component/Themes/ScrollViewer.xaml"/>
                <ResourceDictionary Source="/SunCreate.Common.Controls;Component/Themes/ControlsResource.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Border Margin="10">
                <Button Height="30" Width="80" Content="测试" Style="{StaticResource stlTxtBtn}" HorizontalAlignment="Left" Click="Button_Click" />
            </Border>
            <Border Margin="10">
                <TextBlock Text="测试内容ABC"></TextBlock>
            </Border>
        </StackPanel>
    </Grid>
</ui:WindowEx>
View Code

效果图:

窗体最小化效果图:

 

目录
相关文章
|
8月前
|
C#
WPF疑难问题之Treeview中HierarchicalDataTemplate多级样式
WPF疑难问题之Treeview中HierarchicalDataTemplate多级样式
145 0
|
2月前
|
C#
浅谈WPF之样式与资源
WPF通过样式,不仅可以方便的设置控件元素的展示方式,给用户呈现多样化的体验,还简化配置,避免重复设置元素的属性,以达到节约成本,提高工作效率的目的,样式也是资源的一种表现形式。本文以一个简单的小例子,简述如何设置WPF的样式以及资源的应用,仅供学习分享使用,如有不足之处,还请指正。
42 0
|
8月前
WPF-布局样式练习-Day02-聊天气泡
WPF-布局样式练习-Day02-聊天气泡
129 1
|
4月前
|
数据可视化 API C#
|
6月前
|
人工智能 C#
WPF自定义控件库之Window窗口
本文以自定义窗口为例,简述WPF开发中如何通过自定义控件来扩展功能和样式,仅供学习分享使用,如有不足之处,还请指正。
151 5
|
8月前
|
C#
WPF技术之Xaml Window
WPF Window 是一个 WPF 窗口类,它具有许多属性枚举可以控制窗口的外观和行为。
76 0
WPF技术之Xaml Window
|
8月前
|
C#
WPF-Binding问题-模板样式使用Binding TemplatedParent与TemplateBinding区别
WPF-Binding问题-模板样式使用Binding TemplatedParent与TemplateBinding区别
82 0
|
8月前
WPF-样式问题-处理ListBox、ListView子项内容全填充问题
WPF-样式问题-处理ListBox、ListView子项内容全填充问题
112 0
|
8月前
|
C#
WPF-布局样式练习-Day01
WPF-布局样式练习-Day01
70 0
|
8月前
WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
WPF-样式问题-ListBox或ListView中子项全填充去除边线问题
64 0