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;
        }
       


    }
}


运行结果
image

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值