WPF窗口模板——Style样式

在WPF中新建一个通用窗口模板,在新建一个窗口时,通过使用该模板样式,达成窗口风格统一的目的。以下是具体步骤:

新建一个项目,新建Style文件夹用来存放我们的模板文件,在Style文件夹里新建一个模板文件(资源字典文件)CustomWindow.xaml,同时新建CustomWindow.xaml.cs文件(模板的响应事件处理),此时项目的结构如下:


CustomWindow.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    x:Class="iTools.Style.CustomWindow">
    
    <!-- 菜单按钮组模板 -->
    <Style x:Key="CustomWindowMenuBtn" TargetType="Button">
        <Setter Property="Foreground" Value="White"></Setter>
        <Setter Property="Opacity" Value="0.2"></Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <TextBlock FontSize="25" Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Opacity" Value="1.0"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!-- 通用窗口模板 -->
    <ControlTemplate x:Key="CustomWindowTemplate" TargetType="Window">
        <Border Margin="3" CornerRadius="5,5,5,5" Background="#1BA1E2" MouseLeftButtonDown="CustomWindow_MouseLeftButtonDown">
            <Border.Effect>
                <DropShadowEffect BlurRadius="3" RenderingBias="Performance" ShadowDepth="0" Opacity="1"/>
            </Border.Effect>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="50"></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <Grid Grid.Row="0">
                    <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0"
                                   Text="{TemplateBinding Title}" Foreground="#FFFFFF" FontSize="20"></TextBlock>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,10,0" VerticalAlignment="Top">
                        <Button Height="20" Width="20" Content="-"
                                Style="{StaticResource ResourceKey=CustomWindowMenuBtn}" Click="CustomWindowBtnMinimized_Click" />
                        <Button Height="20" Width="20" Content="□"
                                Style="{StaticResource ResourceKey=CustomWindowMenuBtn}" Click="CustomWindowBtnMaxNormal_Click" />
                        <Button Height="20" Width="20" Content="×"
                                Style="{StaticResource ResourceKey=CustomWindowMenuBtn}" Click="CustomWindowBtnClose_Click" />
                    </StackPanel>
                </Grid>
                <Grid Grid.Row="1">
                    <AdornerDecorator>
                        <ContentPresenter></ContentPresenter>
                    </AdornerDecorator>
                </Grid>
            </Grid>
        </Border>
    </ControlTemplate>

    <!-- 通用窗口样式 -->
    <Style x:Key="CustomWindowChrome" TargetType="Window">
        <Setter Property="AllowsTransparency" Value="True"></Setter>
        <Setter Property="Background" Value="Transparent"></Setter>
        <Setter Property="WindowStyle" Value="None"></Setter>
        <Setter Property="ResizeMode" Value="NoResize"></Setter>
        <Setter Property="Template" Value="{StaticResource CustomWindowTemplate}"></Setter>
    </Style>
    
</ResourceDictionary>

模板xaml中实现了一个无边框窗口,添加了阴影效果,自定义了最大最小化、还原、关闭按钮

CustomWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;

namespace iTools.Style
{
    public partial class CustomWindow
    {
        // 拖动
        private void CustomWindow_MouseLeftButtonDown(object sender, MouseEventArgs e)
        {
            Window win = (Window)((FrameworkElement)sender).TemplatedParent;
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                win.DragMove();
            }
        }

        // 关闭
        private void CustomWindowBtnClose_Click(object sender, RoutedEventArgs e)
        {
            Window win = (Window)((FrameworkElement)sender).TemplatedParent;
            win.Close();
        }

        // 最小化
        private void CustomWindowBtnMinimized_Click(object sender, RoutedEventArgs e)
        {
            Window win = (Window)((FrameworkElement)sender).TemplatedParent;
            win.WindowState = WindowState.Minimized;
        }

        // 最大化、还原
        private void CustomWindowBtnMaxNormal_Click(object sender, RoutedEventArgs e)
        {
            Window win = (Window)((FrameworkElement)sender).TemplatedParent;
            if (win.WindowState == WindowState.Maximized)
            {
                win.WindowState = WindowState.Normal;
            }
            else
            {
                // 不覆盖任务栏
                win.MaxWidth = SystemParameters.WorkArea.Width;
                win.MaxHeight = SystemParameters.WorkArea.Height;
                win.WindowState = WindowState.Maximized;
            }
        }

    }
}

模板的响应事件中,实现了无边框窗口的拖动,和窗口最大最小化、关闭效果。

App.xaml:

<Application x:Class="iTools.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Style/CustomWindow.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

    </Application.Resources>
</Application>

App资源中引用我们的模板文件。

MainWindow.xaml:

<Window x:Class="iTools.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="iTools" Height="350" Width="525" 
        Style="{StaticResource ResourceKey=CustomWindowChrome}">
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="29,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="29,41,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
    </Grid>
</Window>
在主窗口中添加引用模板样式,相同的方法可以新建更多相同风格的窗口:






  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WPF(Windows Presentation Foundation)是一种用于创建Windows应用程序的界面开发技术,其基于.NET Framework平台,并使用C#编程语言进行开发。WPF提供了一套强大的工具和框架,使开发人员能够轻松创建富有吸引力、功能丰富的用户界面。 WPF与传统的Windows Forms相比具有许多优势。首先,WPF支持更加灵活和现代化的用户界面设计,可以轻松地创建透明、动画和多媒体效果等视觉效果。其次,WPF具有更好的分离性,允许开发人员将界面逻辑与业务逻辑进行分离,使代码更加清晰和易于维护。此外,WPF还支持数据绑定和样式模板等功能,使界面开发更加高效和可重用。 在使用WPF进行编程时,首先需要了解XAML(Extensible Application Markup Language)语言,它用于定义WPF界面元素和布局。然后,使用C#语言编写代码逻辑,处理用户交互、数据绑定、事件处理等方面的功能。在WPF中,可以通过使用命令模式和MVVM(Model-View-ViewModel)架构来组织和管理代码,以实现更好的代码分离性。 另外,WPF提供了丰富的控件库,开发人员可以使用这些控件来构建各种功能和复杂的界面。同时,WPF还支持自定义控件的开发,开发人员可以根据具体需求创建自己的控件。 总之,WPF是一种强大的界面开发技术,可以帮助开发人员创建出具有丰富功能和吸引力的Windows应用程序。同时,使用C#语言进行开发可以使开发过程更加高效和灵活。无论是初学者还是有经验的开发人员,都可以从WPF编程宝典中学习到丰富的知识和技巧,提高自己的WPF编程能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值