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
- WPF 的圆角TextBox和有水印的TextBox
- WPF有水印的TextBox(WaterMark)
- WPF的TextBox水印效果详解
- WPF 水印 TextBox MaskedTextBox
- WPF Textbox的使用
- 有textbox的表格
- WPF TextBox的SpellCheck功能
- 【小企】WPF实现TextBox水印效果
- WPF DatePicker 的textbox的焦点
- WPF TextBox绑定数据的简单应用
- wpf 关于textbox的spellcheck无效
- 带清空按钮TextBox的实现(WPF)
- WPF 自定义TextBox(WPF带图片,占位符,清除按钮的TextBox)改造版
- 实现Winform的TextBox自带水印提示信息
- C# 解决为TextBox后台动态添加水印的问题
- C#中TextBox水印提示的实现过程
- textbox有个AutoCompleteType的属性
- Winform 水印TextBox
- VS2010连接MySQL数据库
- QTCreater调试提示ptrace
- php截取某二个特殊字符串间的某段字符串
- EM算法粗浅理解
- 如何利用App Bundle在苹果商店产品页面进行营销
- WPF 的圆角TextBox和有水印的TextBox
- CODVS 开关灯
- android自定义dialog
- protobuf下载地址和使用例子
- ubuntu下快速安装tftp
- WinMain()主函数
- 计蒜客 挑战难题 第9题:元素移除
- Qt 的Graphics View框架
- Bitmap加载于imageView的一些坑