WPF开发-Label自定义背景-Decorator

简介: 首先在App.xaml文件当中添加样式和模板

首先在App.xaml文件当中添加样式和模板

        <!--编辑器通用主题样式--> 
        <Style x:Key="nu_editor_style">
            <Setter Property="Control.Padding" Value="12"></Setter>
            <Setter Property="Control.Background" Value="#FFC1EDF7"></Setter>
            <Setter Property="Control.BorderBrush" Value="#FFA0A2A4"></Setter>
            <Setter Property="Control.BorderThickness" Value="8"></Setter>
            <Setter Property="Control.ClipToBounds" Value="True"></Setter>
        </Style>
        <!--Label 控件模板-->
        <ControlTemplate x:Key="nu_editor_label_template" TargetType="Label" >
            <lib:CustomDrawnDecorator BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"     
           BorderThickness="{TemplateBinding BorderThickness}">

                <ContentPresenter Margin="{TemplateBinding Padding}"
                     HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                     VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                     ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                     Content="{TemplateBinding ContentControl.Content}"
                     RecognizesAccessKey="True" />
            </lib:CustomDrawnDecorator>
        </ControlTemplate>

下面是Decorator的代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    public class CustomDrawnDecorator : Decorator
    {

        double arrowLen = 30d;
        static Thickness defaultBorderThickness = new Thickness(4d);

        public static  DependencyProperty BorderBrushProperty =
           DependencyProperty.Register("BorderBrush", typeof(Brush), typeof(CustomDrawnDecorator), new PropertyMetadata(Brushes.Gray));

        public static  DependencyProperty BorderThicknessProperty =
           DependencyProperty.Register("BorderThickness", typeof(Thickness), typeof(CustomDrawnDecorator), new PropertyMetadata(defaultBorderThickness));

        public static DependencyProperty BackgroundProperty=
        DependencyProperty.Register("Background", typeof(Brush), typeof(CustomDrawnDecorator)
                , new FrameworkPropertyMetadata(Brushes.RosyBrown));

        static CustomDrawnDecorator()
        {

            


        }

        public Brush Background
        {
            get
            {
                return (Brush)GetValue(BackgroundProperty);
            }
            set
            {
                SetValue(BackgroundProperty, value);
            }
        }

        public Brush BorderBrush
        {
            get
            {
                return (Brush)GetValue(BorderBrushProperty);
            }
            set
            {
                SetValue(BorderBrushProperty, value);
            }
        }

        public Thickness BorderThickness
        {
            get { return (Thickness)GetValue(BorderThicknessProperty); }
            set { SetValue(BorderThicknessProperty, value); }
        }

        protected override void OnRender(DrawingContext dc)
        {
            base.OnRender(dc);


            Rect bounds = new Rect(0, 0, base.ActualWidth, base.ActualHeight);
            Pen p = new Pen(BorderBrush,getBorderThickness());
            //填充背景
            dc.DrawRectangle(Background, null, bounds);
            //左长线
            dc.DrawLine(p, new Point(getBorderThickness() / 2, 0), new Point(getBorderThickness() / 2, ActualHeight));
            //右长线
            dc.DrawLine(p, new Point(ActualWidth - getBorderThickness() / 2, 0), new Point(ActualWidth - getBorderThickness() / 2, 
            ActualHeight));
            //左上短线
            dc.DrawLine(p, new Point(0, getBorderThickness() / 2), new Point(arrowLen, getBorderThickness() / 2));
            //右上短线
            dc.DrawLine(p, new Point(ActualWidth - arrowLen, getBorderThickness() / 2), new Point(ActualWidth, getBorderThickness() / 
            2));
            //左下短线
            dc.DrawLine(p, new Point(0, ActualHeight - getBorderThickness() / 2), new Point(arrowLen, ActualHeight - 
            getBorderThickness() / 2));
            //右下短线
            dc.DrawLine(p, new Point(ActualWidth - arrowLen, ActualHeight - getBorderThickness() / 2), new Point(ActualWidth, 
            ActualHeight - getBorderThickness() / 2));

            //dc.DrawRectangle(Background, p, bounds);
            //dc.DrawLine(new Pen(Background, getBorderThickness() / 2), new Point(arrowLen, getBorderThickness() / 4), new 
            //Point(ActualWidth - arrowLen, getBorderThickness() / 4));
            //dc.DrawLine(new Pen(Background, getBorderThickness() / 2), new Point(arrowLen, ActualHeight - getBorderThickness() / 4), 
            //new Point(ActualWidth - arrowLen, ActualHeight - getBorderThickness() / 4));


        }

        public double DpiScaleX { get; }
        private double getBorderThickness()
        {
            return BorderThickness.Right != 0 ? BorderThickness.Right : defaultBorderThickness.Right;
        }
       


    }
}

然后在 mainWindow.xaml文件当中tianji添加Label控件

  <Label Width="300" Height="100"  Style="{StaticResource nu_editor_style}" Template="{StaticResource nu_editor_label_template}"  Content="我是Label控件" FontSize="17"  HorizontalAlignment="Left" Margin="180,185,0,0" VerticalAlignment="Top" />

运行效果
image

目录
相关文章
|
9月前
|
数据库连接 API C#
WPF开发学生信息管理系统【WPF+Prism+MAH+WebApi】(二)(下)
WPF开发学生信息管理系统【WPF+Prism+MAH+WebApi】(二)(下)
141 0
|
5月前
|
程序员 C# 异构计算
一个为程序员定制的、WPF开发的小巧、美观桌面快捷工具
一个为程序员定制的、WPF开发的小巧、美观桌面快捷工具
55 0
|
5月前
|
C# 开发者
一款WPF开发的网易云音乐客户端 - DMSkin-CloudMusic
一款WPF开发的网易云音乐客户端 - DMSkin-CloudMusic
125 36
|
9月前
|
设计模式 前端开发 测试技术
WPF开发之Prism详解【内附源码】
WPF开发之Prism详解【内附源码】
205 0
|
9月前
|
C#
WPF工控组态软件之冷却塔和空气压缩机开发
WPF工控组态软件之冷却塔和空气压缩机开发
170 0
|
9月前
|
C#
WPF工控组态软件之管道和冷却风扇开发
WPF工控组态软件之管道和冷却风扇开发
158 0
|
9月前
|
C# 数据库 C++
WPF开发学生信息管理系统【WPF+Prism+MAH+WebApi】(完)(下)
WPF开发学生信息管理系统【WPF+Prism+MAH+WebApi】(完)(下)
277 0
|
9月前
|
前端开发 C# UED
WPF开发学生信息管理系统【WPF+Prism+MAH+WebApi】(完)(上)
WPF开发学生信息管理系统【WPF+Prism+MAH+WebApi】(完)(上)
178 0
|
9月前
|
C# 数据库
WPF开发学生信息管理系统【WPF+Prism+MAH+WebApi】(四)(下)
WPF开发学生信息管理系统【WPF+Prism+MAH+WebApi】(四)(下)
85 0
|
9月前
|
前端开发 C# 数据库
WPF开发学生信息管理系统【WPF+Prism+MAH+WebApi】(三)(下)
WPF开发学生信息管理系统【WPF+Prism+MAH+WebApi】(三)(下)
164 0