wpf 开发 -TextBox背景自定义-Decorator
首先在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><ControlTemplate x:Key="nu_editor_template" TargetType="{x:Type TextBoxBase}" ><lib:CustomDrawnDecorator BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}"><ScrollViewer Margin="{TemplateBinding Padding}"HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"VerticalAlignment="{TemplateBinding VerticalContentAlignment}"x:Name="PART_ContentHost" /></lib:CustomDrawnDecorator></ControlTemplate>
在mainWindow.xaml文件中添加TextBox控件
<TextBox Width="300" Height="100" Style="{StaticResource nu_editor_style}" Template="{StaticResource nu_editor_template}" HorizontalAlignment="Left" Margin="180,0,0,0" TextWrapping="Wrap" VerticalAlignment="Top" >sdfsd </TextBox>
还有下面是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));}private double getBorderThickness(){return BorderThickness.Right != 0 ? BorderThickness.Right : defaultBorderThickness.Right;}}
}
运行结果
wpf 开发 -TextBox背景自定义-Decorator相关推荐
- WPF 实现TextBox背景中提示文字(水印效果)
网上找到的多是使用VisualBrush,代码如下.单独使用方便,但加入样式模板文字内容无法绑定附加属性(或依赖属性). <TextBox HorizontalAlignment="C ...
- 一个为程序员定制的、WPF开发的小巧、美观桌面快捷工具
今天给大家推荐一个基于WPF开发的,专门为程序员定制的桌面快捷工具. 项目简介 这是基于.Net+WPF开发的,一个小巧.UI美观的快捷工具.此项目发布以来就受到大家的喜欢,代码结构清晰非常适合用来学 ...
- Android软件开发之盘点自定义View界面大合集(二)
Android软件开发之盘点自定义View界面大合集(二) - 雨松MOMO的程序世界 - 51CTO技术博客 雨松MOMO带大家盘点Android 中的自定义View界面的绘制 今天我用自己写的一个 ...
- SharePoint 2013 图文开发系列之自定义字段
原文:SharePoint 2013 图文开发系列之自定义字段 SharePoint使用的优势,就在于开箱即用.快速搭建,SharePoint自身为我们提供了很多字段类型,已经很丰富了.但是,在实际应 ...
- WPF开发必备--类库
1.XamlFlair XamlFlair 库的目标是简化常见动画的实现,并允许开发人员使用几行 Xaml 轻松添加单个或组合的动画集. Github:https://github.com/XamlF ...
- WPF开发学生信息管理系统【WPF+Prism+MAH+WebApi】(完)
最近通过WPF开发项目,为了对WPF知识点进行总结,所以利用业余时间,开发一个学生信息管理系统[Student Information Management System].前四篇文章进行了框架搭建和 ...
- WPF开发学生信息管理系统【WPF+Prism+MAH+WebApi】(一)
最近通过WPF开发项目,为了对WPF知识点进行总结,所以利用业余时间,开发一个学生信息管理系统[Student Information Management System].本文主要简述如何通过WPF ...
- android开发笔记之自定义开关按钮
今天来讲讲自定义单个控件,就拿开关按钮来讲讲,相信大家见了非常多这样的了,先看看效果: 我们可以看到一个很常见的开关按钮,那就来分析分析. 首先: 这是由两张图片构成: ①一张为有开和关的背景图片 ② ...
- C#WPF实现TextBox控件水印效果的两种实现方法
C#WPF实现TextBox控件水印效果的两种实现方法 在WPF实际项目中往往需要在TextBox中加入水印,来告诉使用者输入TextBox中的内容,如下图片所示: 下面介绍两种方式,来添加上图中的水 ...
- 「FastAdmin」fastadmin二次开发中如何自定义查询数据
fastadmin二次开发中如何自定义查询数据 问题背景:最近做一个网站的过程中遇到了一个需求:对于不同用户组的用户,显示的数据要根据权限来筛选.问题看起来不是很难,文档和社区中已经给了足够的提示,我 ...
最新文章
- pandas中DataFrame的ix,loc,iloc索引方式的异同
- Jdon框架开发指南
- 关于Session_End()运行机制的一些细节!
- Android webView 缓存 Cache + HTML5离线功能 解决
- 试用c51语言采样连续5次异常_学会这些自闭症儿童语言训练技巧,孩子开口说话不再困难...
- python单元测试教程_Python单元测试框架unittest使用方法讲解
- 一种连续语音识别系统的制作方法
- 利用DELL的OMSA监控服务器的温度
- Atitit 搜索蓝牙设备 powershell的实现 java noede.js python 先用脚本语言python nodejs,不好实现。。Java 也不好实现。。 Netcore可以,
- 实对称矩阵的特征值求法_梳理:矩阵对角化
- 数据挖掘的9大成熟技术和应用
- IEEE1284 USB转并口打印线缆配置
- 【信息系统项目管理师】第四章 项目整体管理(考点汇总篇)
- vue 组件开发基本思路
- 写给过得很辛苦很迷茫的你~一定要看啊
- 区块链的概念定义是什么
- 希尔贝壳参展世界人工智能大会 | WAIC 2021
- Navicat Premium安装教程(激活)
- Linux网络常用命令
- 人工智能引发了科学研究的革命
热门文章
- 换工作,看机会的,戳进来!
- Linux如何动态查看文件信息,怎么查看linux动态链接库文件的版本等其他信息
- 微型计算机期末总结卷首语,个人总结卷首语.doc
- 没有编程基础可以学python_没有任何编程基础可以直接学习python语言吗?学会后能够做什么?...
- vsan双主机配置_5千右预算,兼顾Pr剪辑、Ps修图、CAD制图的高性价比DIY主机配置...
- linux系统上不去网,linux 上不去网
- python-学生管理系统--9-整体代码
- java案例代码21-电影院购票系统[重要]
- C、C++实现 -- 字符串分割函数split
- Linux DNS 服务器安装、配置和维护