WPF 的圆角TextBox和有水印的TextBox


WPF 中 创建用户自定义控件会自动生成这句代码

 static TextBoxExt()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBoxExt), new FrameworkPropertyMetadata(typeof(TextBoxExt)));
        }
下面代码来源于网络并修改
<pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ZControl
{
    [StyleTypedProperty(Property = "WaterMarkStyle", StyleTargetType = typeof(TextBlock))]
    public class TextBoxExt : TextBox
    {
        TextBlock ChildTB;
        public TextBoxExt()
        {
            VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            GetChildTB();
            if (!string.IsNullOrEmpty(this.Text))
            {
                ChildTB.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void GetChildTB()
        {
            if (ChildTB == null)
            {
                ChildTB = this.GetTemplateChild("TB") as TextBlock;
            }
        }

        protected override void OnGotFocus(RoutedEventArgs e)
        {
            ChildTB.Visibility = System.Windows.Visibility.Collapsed;
            base.OnGotFocus(e);
        }

        protected override void OnLostFocus(RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.Text))
            {
                ChildTB.Visibility = System.Windows.Visibility.Visible;
            }
            base.OnLostFocus(e);
        }

        static TextBoxExt()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBoxExt), new FrameworkPropertyMetadata(typeof(TextBoxExt)));
        }

        public Brush MouseOverBorderBrush
        {
            get;
            set;
        }

        public Brush MouseOverBackgroundBrush
        {
            get;
            set;
        }

        public Brush FocusedBorderBrush
        {
            get;
            set;
        }

        public string WaterMark
        {
            get;
            set;
        }

        public Style WaterMarkStyle
        {
            get;
            set;
        }
    }
}

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:z="clr-namespace:ZControl"
                    >
    <Style x:Key="TextBoxExtRectangle" TargetType="{x:Type Rectangle}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Stroke" Value="Red"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
    <Style TargetType="{x:Type z:TextBoxExt}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type z:TextBoxExt}">
                    <Grid>
                        <Rectangle Style="{StaticResource TextBoxExtRectangle}"  RadiusX="5" RadiusY="5" Fill="{TemplateBinding Background}" Stroke="OrangeRed" StrokeThickness="{TemplateBinding BorderThickness}">
                        </Rectangle>
                        <TextBlock x:Name="TB" VerticalAlignment="Center"
                                               Style="{Binding RelativeSource={RelativeSource Mode= FindAncestor, AncestorType={x:Type z:TextBoxExt}}, Path=WaterMarkStyle }"  
                                               HorizontalAlignment="Center" 
                                               Text="{Binding RelativeSource={RelativeSource Mode= FindAncestor, AncestorType={x:Type z:TextBoxExt}}, Path=WaterMark }">

                        </TextBlock>
                        <ScrollViewer x:Name="PART_ContentHost"
                                         HorizontalScrollBarVisibility="Auto"
                                         VerticalScrollBarVisibility="Auto"
                                         Padding="{TemplateBinding Padding}">
                        </ScrollViewer>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>


 
 

这句代码的作用是不去寻找对应的 xaml 文件,样式使用 <style TargetType='XXX'> 作为模板

WPF中 绑定的类型

1.{TemplateBinding Background} TemplateBinding 
绑定使用模板的类中的一个属性。这个属性是依赖属性否则会报错
2.{Binding RelativeSource={RelativeSource Mode= FindAncestor, AncestorType={x:Type z:TextBoxExt}}, Path=WaterMark }
使用用RelativeSource寻找数据源类
Mode 类型 1. FindAncestor 寻找上级。配合 AncestorType 使用指定上级的类型
 
 
 2.Self 自身作为数据源
 3.TemplatedParent 
{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值