WPF 的圆角TextBox和有水印的TextBox

来源:互联网 发布:如何下载99宿舍软件 编辑:程序博客网 时间:2024/05/01 17:20

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}

0 0
原创粉丝点击