自定义Window窗体样式

本文介绍了如何在WPF中自定义Window窗体样式,包括取消默认样式、设置窗体透明以及创建自定义的标题栏内容模板。通过示例代码展示了如何注册依赖属性和绑定命令,提供了一种基础样式(BaseWindowStyle)和默认样式(DefaultWindowStyle),以满足不同定制需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为WPF默认的窗体比较简陋,大都需要自己实现Window窗体样式效果,基本思路:

第一步:取消默认样式:WindowStyle = WindowStyle.None;

第二步:设置窗体透明:AllowsTransparency = true;

第三步:设置自己的窗体样式;

代码:

    #region CaptionForeground 标题栏前景景色

       public static readonly DependencyProperty CaptionForegroundProperty = DependencyProperty.Register(

           "CaptionForeground", typeof(Brush), typeof(WindowBase), new PropertyMetadata(null));

        public Brush CaptionForeground

        {

            get { return (Brush)GetValue(CaptionForegroundProperty); }

            set { SetValue(CaptionForegroundProperty, value); }

        }

        #endregion

#region Header 标题栏内容模板,以提高默认模板,可自定义

        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(

            "Header", typeof(ControlTemplate), typeof(WindowBase), new PropertyMetadata(null));

        public ControlTemplate Header

        {

            get { return (ControlTemplate)GetValue(HeaderProperty); }

            set { SetValue(HeaderProperty, value); }

        }

      #endregion

绑定命令的扩展方法:

public static void BindCommand(this UIElement @ui, ICommand com, Action<object, ExecutedRoutedEventArgs> call){

            var bind = new CommandBinding(com);

            bind.Executed += new ExecutedRoutedEventHandler(call);

            @ui.CommandBindings.Add(bind);

        }

 WindowBase的样式有两个,一个是基础样式BaseWindowStyle,可以自定义头部标题栏,提供更多定制需求。另一个样式DefaultWindowStyle为默认窗体样式,用于大多数不要特别定制的窗体需求。BaseWindowStyle样式代码:  

<Setter Property = "Template" >

            < Setter.Value >

                < ControlTemplate TargetType="{x:Type local:WindowBase}">

                    <Grid Margin = "6" >

                        < !--背景-- >

                        < Border x:Name="Bg" CornerRadius="{TemplateBinding local:ControlAttachProperty.CornerRadius}"

                            Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" Effect="{StaticResource WindowDropShadow}"

                  <Grid Margin = "1" >

                            < Grid.RowDefinitions >

                                < RowDefinition MinHeight="18" Height="Auto"/>

                                <RowDefinition Height = "*" />

                            </ Grid.RowDefinitions >

                            < !--Caption标题部分-- >

                     < Border Margin="0" Grid.Row="0" Background="{TemplateBinding CaptionBackground}">

                             <Grid Margin = "2,1,2,0" >

                                    < Grid.ColumnDefinitions >

                                        < ColumnDefinition Width="*"/>

                                        <ColumnDefinition Width = "Auto" />

                                    </ Grid.ColumnDefinitions >

                              </ Grid >

                            </ Border >

                            < AdornerDecorator Grid.Row= "1" Margin= "3,0,3,3" >

                                < ContentPresenter Content= "{TemplateBinding Content}" />

                            </ AdornerDecorator >

                        </ Grid >

                    </ Grid >

                    < ControlTemplate.Triggers >

                        < Trigger Property= "WindowState" Value= "Maximized" >

< Setter Property= "FIcon" TargetName= "btnMax" Value= "&#xe62b;" ></ Setter >

                        </ Trigger >

                        < Trigger Property= "WindowState" Value= "Normal" >

< Setter Property= "FIcon" TargetName= "btnMax" Value= "&#xe65b;" ></ Setter >

                        </ Trigger >

                    </ ControlTemplate.Triggers >

                </ ControlTemplate >

            </ Setter.Value >

        </ Setter >

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值