WPF MVVM 模式下自写自用的窗口样式
废话我也就不多说,直接上菜(由于公司电脑做了加密,无法把代码压缩发布,只能以这种方式来分享)
先来看下窗口图
空白区域就是自己想展现的内容
再来看看动图
- 总共分为五块
1.窗口
1.1前端代码
<Window x:Class="WindowDemo.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:WindowDemo"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:mvvm="clr-namespace:WindowDemo.mvvmhelper"
WindowStyle="None" ResizeMode="NoResize" Background="Transparent" OpacityMask="White" AllowsTransparency="True" WindowStartupLocation="CenterScreen">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="style\Style_Button.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Border CornerRadius="10" BorderThickness="1" Background="#ffffff" Margin="{Binding Window_Margin}" >
<Border.Effect >
<DropShadowEffect BlurRadius="15" ShadowDepth="0.5" Opacity="0.5" />
</Border.Effect>
<Grid Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<!--标题-->
<Border Grid.Row="0" CornerRadius="{Binding Max_Top_CornerRadius}" Background="#1296DB">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<mvvm:EventCommand Command="{Binding MouseDown}"/>
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<mvvm:EventCommand Command="{Binding PreviewMouseLeftButtonDown}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Border.Effect>
<DropShadowEffect BlurRadius="5" ShadowDepth="0.5" Opacity="1" />
</Border.Effect>
<!--标题-->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40"/>
<ColumnDefinition/>
<ColumnDefinition Width="{Binding Min_Width}"/>
<ColumnDefinition Width="{Binding Max_Width}"/>
<ColumnDefinition Width="{Binding Close_Width}"/>
</Grid.ColumnDefinitions>
<!--LOGO-->
<Image Grid.Column="0" Source="image/window/logo.png" Width="30"/>
<!--标题-->
<Label Grid.Column="1" Content="{Binding Title_Content}" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="#EDFFFC" FontWeight="Bold" FontSize="16" />
<!--最小化-->
<Button Grid.Column="2" Background="{x:Null}" BorderThickness="0" Command="{Binding Min_Command}" Template="{DynamicResource ButtonIsMouseOver}" Visibility="{Binding Min_Visibility}" IsEnabled="{Binding Min_IsEnabled}">
<StackPanel Orientation="Horizontal">
<Image Source="image/window/Min.png" Width="20" />
</StackPanel>
</Button>
<!--最大化-->
<Button Grid.Column="3" Background="{x:Null}" BorderThickness="0" Command="{Binding Max_Command}" Template="{DynamicResource ButtonIsMouseOver}" Visibility="{Binding Max_Visibility}" IsEnabled="{Binding Max_IsEnabled}">
<StackPanel Orientation="Horizontal">
<Image Source="image/window/Max.png" Width="20" />
</StackPanel>
</Button>
<!--关闭-->
<Button Grid.Column="4" Background="{x:Null}" BorderThickness="0" Command="{Binding Close_Command}" Visibility="{Binding Close_Visibility}" IsEnabled="{Binding Close_IsEnabled}">
<StackPanel Orientation="Horizontal">
<Image Source="image/window/Close.png" Width="20" />
</StackPanel>
<Button.Template>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Border CornerRadius="{Binding Max_Close_CornerRadius}" x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsDefaulted" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="#24c2f8"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</Border>
<!--内容-->
<Grid Grid.Row="1">
</Grid>
<!--版本-->
<Border Grid.Row="2" CornerRadius="{Binding Max_Bottom_CornerRadius}" Background="#1296DB">
<Border.Effect>
<DropShadowEffect BlurRadius="5" ShadowDepth="0.5" Opacity="1" />
</Border.Effect>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="25"/>
<ColumnDefinition Width="41"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="image/window/Ver.png" />
<Label Grid.Column="1" Content="Ver : " VerticalAlignment="Center" FontWeight="Bold" Foreground="#FFFFFF" FontSize="13"/>
<Label Grid.Column="2" Content="{Binding Ver_Content}" VerticalAlignment="Center" FontWeight="Bold" Foreground="#E81A1C" FontSize="13"/>
</Grid>
</Border>
</Grid>
</Border>
</Window>
Copy
1.2后端代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WindowDemo.controller;
namespace WindowDemo
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
public MainWindow()
InitializeComponent();
this.DataContext = new MainWindowController(this);
}
Copy
2.样式
2.1按钮样式【隐藏、放大、关闭】
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Width" Value="100"/>
<Setter Property="Height" Value="30"/>
<Setter Property="Background" Value="#F7F7F7"/>
<Setter Property="BorderBrush" Value="#C4C7CE"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="True"/>
</Style.Triggers>
</Style>
<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonStyle}">
<Setter Property="Width" Value="95"/>
</Style>
<Style x:Key="ButtonStyle2" TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonStyle}">
<Setter Property="Width" Value="80"/>
<Setter Property="Height" Value="40"/>
</Style>
<Style x:Key="ButtonImageStyle" TargetType="{x:Type Image}">
<Setter Property="Stretch" Value="Fill"/>
<Setter Property="Margin" Value="0,0,5,0"/>
<Setter Property="Width" Value="16"/>
<Setter Property="Height" Value="16"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style x:Key="ButtonImageStyle_Hede" TargetType="{x:Type Image}">
<Setter Property="Stretch" Value="Fill"/>
<Setter Property="Margin" Value="0,0,5,0"/>
<Setter Property="Width" Value="20"/>
<Setter Property="Height" Value="20"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style x:Key="ButtonTextBlockStyle" TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="13"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Width" Value="auto"/>
<Setter Property="Foreground" Value="#5696DD"/>
</Style>
<!-- 悬停状态的画刷 -->
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
<SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
<SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
<SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
<SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
<SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>
<ControlTemplate x:Key="ButtonTemplate1" TargetType="{x:Type ButtonBase}">
<Border x:Name="border" CornerRadius="3" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsDefaulted" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<LinearGradientBrush x:Key="IsMouseOver_BackgroundBrushKey" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFFFF" Offset="0" />
<GradientStop Color="#E5F3FB" Offset="1" />
</LinearGradientBrush>
<SolidColorBrush x:Key="IsMouseOver_BorderBrushKey" Color="#70C0E7" />
<!-- 按下状态的画刷 -->
<LinearGradientBrush x:Key="IsPressed_BackgroundBrushKey" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFFFFF" Offset="0" />
<GradientStop Color="#D1E8FF" Offset="1" />
</LinearGradientBrush>
<SolidColorBrush x:Key="IsPressed_BorderBrushKey" Color="#66A7E8" />
<!--END-->
<Style x:Key="MenuButton" TargetType="{x:Type Button}" >
<Setter Property="BorderBrush" Value="#E9ECF1"></Setter>
<Setter Property="BorderThickness" Value="1"></Setter>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<ContentPresenter x:Name="contentPresenter"
Focusable="False"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource IsMouseOver_BorderBrushKey}" />
<Setter TargetName="Border" Property="Background" Value="{StaticResource IsMouseOver_BackgroundBrushKey}" />
<!--<Setter TargetName="Border" Property="BorderThickness" Value="0,1,0,1"/>-->
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource IsPressed_BorderBrushKey}" />
<Setter TargetName="Border" Property="Background" Value="{StaticResource IsPressed_BackgroundBrushKey}" />
<!--<Setter TargetName="Border" Property="BorderThickness" Value="0,1,0,1"/>-->
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<ControlTemplate x:Key="ButtonIsMouseOver" TargetType="{x:Type ButtonBase}">
<Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsDefaulted" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="#24c2f8"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ResourceDictionary>
Copy
3.MVVM模式的扩展方法
3.1命令
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace WindowDemo.mvvmhelper
/// <summary>
/// 命令
/// </summary>
public class Command : ICommand
/// <summary>
/// 检查命令是否可以执行的事件,在UI事件发生导致控件状态或数据发生变化时触发
/// </summary>
public event EventHandler CanExecuteChanged
if (_canExecute != null)
CommandManager.RequerySuggested += value;
remove
if (_canExecute != null)
CommandManager.RequerySuggested -= value;
/// <summary>
/// 判断命令是否可以执行的方法
/// </summary>
private Func<object, bool> _canExecute;
/// <summary>
/// 命令需要执行的方法
/// </summary>
private Action<object> _execute;
/// <summary>
/// 创建一个命令
/// </summary>
/// <param name="execute">命令要执行的方法</param>
public Command(Action<object> execute) : this(execute, null)
/// <summary>
/// 创建一个命令
/// </summary>
/// <param name="execute">命令要执行的方法</param>
/// <param name="canExecute">判断命令是否能够执行的方法</param>
public Command(Action<object> execute, Func<object, bool> canExecute)
_execute = execute;
_canExecute = canExecute;
/// <summary>
/// 判断命令是否可以执行
/// </summary>
/// <param name="parameter">命令传入的参数</param>
/// <returns>是否可以执行</returns>
public bool CanExecute(object parameter)
if (_canExecute == null) return true;
return _canExecute(parameter);
/// <summary>
/// 执行命令
/// </summary>
/// <param name="parameter"></param>
public void Execute(object parameter)
if (_execute != null && CanExecute(parameter))
_execute(parameter);
public class Command<T> : ICommand
/// <summary>
/// 检查命令是否可以执行的事件,在UI事件发生导致控件状态或数据发生变化时触发
/// </summary>
public event EventHandler CanExecuteChanged
if (_canExecute != null)
CommandManager.RequerySuggested += value;
remove
if (_canExecute != null)
CommandManager.RequerySuggested -= value;
/// <summary>
/// 判断命令是否可以执行的方法
/// </summary>
private Func<T, bool> _canExecute;
/// <summary>
/// 命令需要执行的方法
/// </summary>
private Action<T> _execute;
/// <summary>
/// 创建一个命令
/// </summary>
/// <param name="execute">命令要执行的方法</param>
public Command(Action<T> execute) : this(execute, null)
/// <summary>
/// 创建一个命令
/// </summary>
/// <param name="execute">命令要执行的方法</param>
/// <param name="canExecute">判断命令是否能够执行的方法</param>
public Command(Action<T> execute, Func<T, bool> canExecute)
_execute = execute;
_canExecute = canExecute;
/// <summary>
/// 判断命令是否可以执行
/// </summary>
/// <param name="parameter">命令传入的参数</param>
/// <returns>是否可以执行</returns>
public bool CanExecute(object parameter)
if (_canExecute == null) return true;
return _canExecute((T)parameter);
/// <summary>
/// 执行命令
/// </summary>
/// <param name="parameter"></param>
public void Execute(object parameter)
if (_execute != null && CanExecute(parameter))
_execute((T)parameter);
}
Copy
3.2事件命令
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
namespace WindowDemo.mvvmhelper
/// <summary>
/// 事件命令 需要到nuget 安装 System.Windows.Interactivity
/// </summary>
public class EventCommand : TriggerAction<DependencyObject>
/// <summary>
/// 事件要绑定的命令
/// </summary>
public ICommand Command
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
// Using a DependencyProperty as the backing store for MsgName. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(EventCommand), new PropertyMetadata(null));
/// <summary>
/// 绑定命令的参数,保持为空就是事件的参数
/// </summary>
public object CommandParateter
get { return (object)GetValue(CommandParateterProperty); }
set { SetValue(CommandParateterProperty, value); }
// Using a DependencyProperty as the backing store for CommandParateter. This enables animation, styling, binding, etc...
public static readonly DependencyProperty CommandParateterProperty =
DependencyProperty.Register("CommandParateter", typeof(object), typeof(EventCommand), new PropertyMetadata(null));
//执行事件
protected override void Invoke(object parameter)
if (CommandParateter != null)
parameter = CommandParateter;
var cmd = Command;
if (cmd != null)
cmd.Execute(parameter);
}
Copy
3.3通知
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowDemo.mvvmhelper
/// <summary>
/// 通知对象
/// </summary>
public class NotifyObject : INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// 属性发生改变时调用该方法发出通知
/// </summary>
/// <param name="propertyName">属性名称</param>
public void RaisePropertyChanged(string propertyName)
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
protected virtual void SetAndNotifyIfChanged<T>(string propertyName, ref T oldValue, T newValue)
if (oldValue == null && newValue == null) return;
if (oldValue != null && oldValue.Equals(newValue)) return;
if (newValue != null && newValue.Equals(oldValue)) return;
oldValue = newValue;
RaisePropertyChanged(propertyName);
}
Copy
4.图片
4.1按钮图片【隐藏、放大、关闭、版本、logo】【传送门: https://www.iconfont.cn/ 】
5.ViewModel
5.1主窗口的viewmodel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WindowDemo.controller
/// <summary>
/// MVVM模式绑定
/// </summary>
public class MainWindowController : WindowController
public MainWindowController(FrameworkElement ThisWindow)
this.ThisWindow = ThisWindow;
Title_Content = "「 Shunnet.top 」演示窗口";
}
Copy
5.2 专门控制主窗口的viewmodel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using WindowDemo.mvvmhelper;
namespace WindowDemo.controller
/// <summary>
/// 窗体控制器
/// </summary>
public class WindowController : NotifyObject
/// <summary>
/// 当前窗口
/// </summary>
public FrameworkElement ThisWindow { get; set; }
/// <summary>
/// 窗体边距
/// </summary>
public Thickness Window_Margin
return window_Margin;
if (!value.Equals(window_Margin))
window_Margin = value;
RaisePropertyChanged("Window_Margin");
private Thickness window_Margin = new Thickness(20);
/// <summary>
/// 最小化按钮是否显示
/// </summary>
public Visibility Min_Visibility
return min_Visibility;
if (!value.Equals(min_Visibility))
switch (value)
case Visibility.Visible:
Min_Width = 40;
break;
case Visibility.Hidden:
case Visibility.Collapsed:
Min_Width = 0;
break;
min_Visibility = value;
RaisePropertyChanged("Min_Visibility");
private Visibility min_Visibility = Visibility.Visible; //默认显示
/// <summary>
/// 最小化按钮被点击
/// </summary>
public Command Min_Command
if (min_Command == null)
min_Command = new Command(new Action<object>
((Window)ThisWindow).WindowState = WindowState.Minimized;
return min_Command;
private Command min_Command;
/// <summary>
/// 最小化按钮宽度
/// </summary>
public double Min_Width
return min_Width;
if (!value.Equals(min_Width))
min_Width = value;
RaisePropertyChanged("Min_Width");
private double min_Width = 40;
/// <summary>
/// 最大化按钮是否显示
/// </summary>
public Visibility Max_Visibility
return max_Visibility;
if (!value.Equals(max_Visibility))
switch (value)
case Visibility.Visible:
Max_Width = 40;
break;
case Visibility.Hidden:
case Visibility.Collapsed:
Max_Width = 0;
break;
max_Visibility = value;
RaisePropertyChanged("Max_Visibility");
private Visibility max_Visibility = Visibility.Visible; //默认显示
/// <summary>
/// 最大化按钮被点击
/// </summary>
public Command Max_Command
if (max_Command == null)
max_Command = new Command(new Action<object>
MaxOrMinWindow();
return max_Command;
/// <summary>
/// 最大化、小化
/// </summary>
private void MaxOrMinWindow()
switch (((Window)ThisWindow).WindowState)
case WindowState.Normal:
Max_Close_CornerRadius = new CornerRadius(0);
Max_Top_CornerRadius = new CornerRadius(0);
Max_Bottom_CornerRadius = new CornerRadius(0);
Window_Margin = new Thickness(0);
((Window)ThisWindow).WindowState = WindowState.Maximized;
break;
case WindowState.Maximized:
Max_Close_CornerRadius = new CornerRadius(0, 10, 0, 0);
Max_Top_CornerRadius = new CornerRadius(10, 10, 0, 0);
Max_Bottom_CornerRadius = new CornerRadius(0, 0, 10, 10);
Window_Margin = new Thickness(20);
((Window)ThisWindow).WindowState = WindowState.Normal;
break;
private Command max_Command;
/// <summary>
/// 最大化按钮的宽度
/// </summary>
public double Max_Width
return max_Width;
if (!value.Equals(max_Width))
max_Width = value;
RaisePropertyChanged("Max_Width");
private double max_Width = 40;
/// <summary>
/// 关闭按钮是否显示
/// </summary>
public Visibility Close_Visibility
return close_Visibility;
if (!value.Equals(close_Visibility))
switch (value)
case Visibility.Visible:
Close_Width = 40;
break;
case Visibility.Hidden:
case Visibility.Collapsed:
Close_Width = 0;
break;
close_Visibility = value;
RaisePropertyChanged("Close_Visibility");
private Visibility close_Visibility = Visibility.Visible; //默认显示
/// <summary>
/// 关闭按钮被点击
/// </summary>
public Command Close_Command
if (close_Command == null)
close_Command = new Command(new Action<object>
((Window)ThisWindow).Close();
Environment.Exit(0);
return close_Command;
private Command close_Command;
/// <summary>
/// 关闭按钮的宽度
/// </summary>
public double Close_Width
return close_Width;
if (!value.Equals(close_Width))
close_Width = value;
RaisePropertyChanged("Close_Width");
private double close_Width = 40;
/// <summary>
/// 标题内容
/// </summary>
public string Title_Content
return title_Content;
if (!value.Equals(title_Content))
title_Content = value;
RaisePropertyChanged("Title_Content");
private string title_Content = string.Empty; //默认为空
/// <summary>
/// 软件版本
/// </summary>
public string Ver_Content
return ver_Content;
if (!value.Equals(ver_Content))
ver_Content = value;
RaisePropertyChanged("Ver_Content");
private string ver_Content = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
/// <summary>
/// 鼠标按下 实现窗口移动
/// </summary>
public Command<MouseButtonEventArgs> MouseDown
if (mouseDown == null)
mouseDown = new Command<MouseButtonEventArgs>(
new Action<MouseButtonEventArgs>(e =>
((Window)ThisWindow).DragMove();
return mouseDown;
public Command<MouseButtonEventArgs> mouseDown;
/// <summary>
/// 点击数量
/// </summary>
int CkickCount = 0;
/// <summary>
/// 实现双击 最大化
/// </summary>
public Command<MouseButtonEventArgs> PreviewMouseLeftButtonDown
if (previewMouseLeftButtonDown == null)
previewMouseLeftButtonDown = new Command<MouseButtonEventArgs>(
new Action<MouseButtonEventArgs>(e =>
++CkickCount;
DispatcherTimer ClickTimer = new DispatcherTimer();
ClickTimer.Interval = new TimeSpan(0, 0, 0, 0, 200);
ClickTimer.Tick += (s, e1) =>
ClickTimer.IsEnabled = false;
CkickCount = 0;
ClickTimer.IsEnabled = true;
if (CkickCount % 2 == 0)
ClickTimer.IsEnabled = false;
CkickCount = 0;
DoubleClickCommand();
return previewMouseLeftButtonDown;
public Command<MouseButtonEventArgs> previewMouseLeftButtonDown;
/// <summary>
/// 双击
/// </summary>
private void DoubleClickCommand()
if (Max_IsEnabled)
MaxOrMinWindow();
/// <summary>
/// 当窗体最大化顶部圆角边
/// </summary>
public CornerRadius Max_Top_CornerRadius
return max_Top_CornerRadius;
if (!value.Equals(max_Top_CornerRadius))
max_Top_CornerRadius = value;
RaisePropertyChanged("Max_Top_CornerRadius");
private CornerRadius max_Top_CornerRadius=new CornerRadius(10,10,0,0);
/// <summary>
/// 当窗体最大化底部圆角边
/// </summary>
public CornerRadius Max_Bottom_CornerRadius
return max_Bottom_CornerRadius;
if (!value.Equals(max_Bottom_CornerRadius))
max_Bottom_CornerRadius = value;
RaisePropertyChanged("Max_Bottom_CornerRadius");
private CornerRadius max_Bottom_CornerRadius = new CornerRadius(0,0,10,10);
/// <summary>
/// 当窗体最大化关闭按钮圆角边
/// </summary>
public CornerRadius Max_Close_CornerRadius
return max_Close_CornerRadius;
if (!value.Equals(max_Close_CornerRadius))
max_Close_CornerRadius = value;
RaisePropertyChanged("Max_Close_CornerRadius");
private CornerRadius max_Close_CornerRadius = new CornerRadius(0, 10, 0, 0);
/// <summary>
/// 关闭按钮是否使用
/// </summary>
public bool Min_IsEnabled
return min_IsEnabled;
if (!value.Equals(min_IsEnabled))
min_IsEnabled = value;
RaisePropertyChanged("Min_IsEnabled");
private bool min_IsEnabled = true;
/// <summary>
/// 关闭按钮是否使用
/// </summary>
public bool Max_IsEnabled
return max_IsEnabled;
if (!value.Equals(max_IsEnabled))
max_IsEnabled = value;
RaisePropertyChanged("Max_IsEnabled");
private bool max_IsEnabled = true;
/// <summary>
/// 关闭按钮是否使用
/// </summary>
public bool Close_IsEnabled
return close_IsEnabled;
if (!value.Equals(close_IsEnabled))
close_IsEnabled = value;
RaisePropertyChanged("Close_IsEnabled");