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天前
|
C# 开发者 Windows
WPF 应用程序开发:一分钟入门
本文介绍 Windows Presentation Foundation (WPF),这是一种用于构建高质量、可缩放的 Windows 桌面应用程序的框架,支持 XAML 语言,方便 UI 设计与逻辑分离。文章涵盖 WPF 基础概念、代码示例,并深入探讨常见问题及解决方案,包括数据绑定、控件样式与模板、布局管理等方面,帮助开发者高效掌握 WPF 开发技巧。
137 65
|
2月前
|
开发框架 前端开发 JavaScript
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(12) -- 使用代码生成工具Database2Sharp生成WPF界面代码
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(12) -- 使用代码生成工具Database2Sharp生成WPF界面代码
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(12) -- 使用代码生成工具Database2Sharp生成WPF界面代码
|
2月前
|
开发框架 缓存 前端开发
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(11) -- 下拉列表的数据绑定以及自定义系统字典列表控件
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(11) -- 下拉列表的数据绑定以及自定义系统字典列表控件
|
2月前
|
测试技术 C# 开发者
“代码守护者:详解WPF开发中的单元测试策略与实践——从选择测试框架到编写模拟对象,全方位保障你的应用程序质量”
【8月更文挑战第31天】单元测试是确保软件质量的关键实践,尤其在复杂的WPF应用中更为重要。通过为每个小模块编写独立测试用例,可以验证代码的功能正确性并在早期发现错误。本文将介绍如何在WPF项目中引入单元测试,并通过具体示例演示其实施过程。首先选择合适的测试框架如NUnit或xUnit.net,并利用Moq模拟框架隔离外部依赖。接着,通过一个简单的WPF应用程序示例,展示如何模拟`IUserRepository`接口并验证`MainViewModel`加载用户数据的正确性。这有助于确保代码质量和未来的重构与扩展。
30 0
|
2月前
|
前端开发 C# 设计模式
“深度剖析WPF开发中的设计模式应用:以MVVM为核心,手把手教你重构代码结构,实现软件工程的最佳实践与高效协作”
【8月更文挑战第31天】设计模式是在软件工程中解决常见问题的成熟方案。在WPF开发中,合理应用如MVC、MVVM及工厂模式等能显著提升代码质量和可维护性。本文通过具体案例,详细解析了这些模式的实际应用,特别是MVVM模式如何通过分离UI逻辑与业务逻辑,实现视图与模型的松耦合,从而优化代码结构并提高开发效率。通过示例代码展示了从模型定义、视图模型管理到视图展示的全过程,帮助读者更好地理解并应用这些模式。
58 0
|
2月前
|
容器 C# Docker
WPF与容器技术的碰撞:手把手教你Docker化WPF应用,实现跨环境一致性的开发与部署
【8月更文挑战第31天】容器技术简化了软件开发、测试和部署流程,尤其对Windows Presentation Foundation(WPF)应用程序而言,利用Docker能显著提升其可移植性和可维护性。本文通过具体示例代码,详细介绍了如何将WPF应用Docker化的过程,包括创建Dockerfile及构建和运行Docker镜像的步骤。借助容器技术,WPF应用能在任何支持Docker的环境下一致运行,极大地提升了开发效率和部署灵活性。
48 0
|
2月前
|
区块链 C# 存储
链动未来:WPF与区块链的创新融合——从智能合约到去中心化应用,全方位解析开发安全可靠DApp的最佳路径
【8月更文挑战第31天】本文以问答形式详细介绍了区块链技术的特点及其在Windows Presentation Foundation(WPF)中的集成方法。通过示例代码展示了如何选择合适的区块链平台、创建智能合约,并在WPF应用中与其交互,实现安全可靠的消息存储和检索功能。希望这能为WPF开发者提供区块链技术应用的参考与灵感。
46 0
|
2月前
|
开发者 C# Windows
WPF与游戏开发:当桌面应用遇见游戏梦想——利用Windows Presentation Foundation打造属于你的2D游戏世界,从环境搭建到代码实践全面解析新兴开发路径
【8月更文挑战第31天】随着游戏开发技术的进步,WPF作为.NET Framework的一部分,凭借其图形渲染能力和灵活的UI设计,成为桌面游戏开发的新选择。本文通过技术综述和示例代码,介绍如何利用WPF进行游戏开发。首先确保安装最新版Visual Studio并创建WPF项目。接着,通过XAML设计游戏界面,并在C#中实现游戏逻辑,如玩家控制和障碍物碰撞检测。示例展示了创建基本2D游戏的过程,包括角色移动和碰撞处理。通过本文,WPF开发者可更好地理解并应用游戏开发技术,创造吸引人的桌面游戏。
98 0
|
2月前
|
开发者 C# 自然语言处理
WPF开发者必读:掌握多语言应用程序开发秘籍,带你玩转WPF国际化支持!
【8月更文挑战第31天】随着全球化的加速,开发多语言应用程序成为趋势。WPF作为一种强大的图形界面技术,提供了优秀的国际化支持,包括资源文件存储、本地化处理及用户界面元素本地化。本文将介绍WPF国际化的实现方法,通过示例代码展示如何创建和绑定资源文件,并设置应用程序语言环境,帮助开发者轻松实现多语言应用开发,满足不同地区用户的需求。
44 0
|
2月前
|
开发者 C# UED
WPF多窗口应用程序开发秘籍:掌握窗口创建、通信与管理技巧,轻松实现高效多窗口协作!
【8月更文挑战第31天】在WPF应用开发中,多窗口设计能显著提升用户体验与工作效率。本文详述了创建新窗口的多种方法,包括直接实例化`Window`类、利用`Application.Current.MainWindow`及自定义方法。针对窗口间通信,介绍了`Messenger`类、`DataContext`共享及`Application`类的应用。此外,还探讨了布局控件与窗口管理技术,如`StackPanel`与`DockPanel`的使用,并提供了示例代码展示如何结合`Messenger`类实现窗口间的消息传递。总结了多窗口应用的设计要点,为开发者提供了实用指南。
101 0