WPF Window 样式
一、无边框窗体
XAML样式如下:
<!--无边框窗体样式 START--> <Style TargetType="Window" x:Key="WindowStyle"> <!--无边框--> <Setter Property="WindowStyle" Value="None"></Setter> <!--窗体设置--> <Setter Property="WindowChrome.WindowChrome"> <Setter.Value> <!--设置为"-1"才能达到阴影效果--> <WindowChrome GlassFrameThickness="-1" CaptionHeight="0"/> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Window"> <Border Background="White"> <Border.Effect> <!--阴影效果--> <DropShadowEffect ShadowDepth="0" BlurRadius="10" Opacity="0.8" Color="#AAA"/> </Border.Effect> <ContentPresenter ClipToBounds="True" /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--无边框窗体样式 END-->
二、背景透明窗体
XAML样式如下:
<!--背景透明窗体样式 START--> <Style TargetType="Window" x:Key="TransparentWindowStyle"> <!--无边框--> <Setter Property="WindowStyle" Value="None"></Setter> <Setter Property="ResizeMode" Value="CanMinimize"></Setter> <!--窗体设置--> <Setter Property="WindowChrome.WindowChrome"> <Setter.Value> <!--设置为"-1"才能达到自定义效果--> <WindowChrome GlassFrameThickness="-1" CaptionHeight="0"/> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Window"> <Border Background="Transparent"/> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--背景透明窗体样式 END-->
三、窗体默认加载
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //屏幕居中显示 this.WindowStartupLocation = WindowStartupLocation.CenterScreen; //MVVM数据源 this.DataContext = MainWindowViewModel.CreateInstance(); //拖拽事件 this.MouseDown += (s, e) => { base.OnMouseLeftButtonDown(e); if (Mouse.LeftButton == MouseButtonState.Pressed) { DragMove(); } }; } }