WPF 实现带标题的TextBox
这篇博客将分享在WPF中如何创建一个带Title的TextBox。首先请看一下最终的效果,

实现思路:使用TextBlock+TextBox来实现,TextBlock用来显示Title。
实现代码,
TitleTextBox
[TemplatePart(Name = TitleTextBlockKey, Type = typeof(TextBlock))]
public class TitleTextBox : TextBox
{
private const string TitleTextBlockKey = "PART_TitleTextBlock"; private TextBlock _tbkTitle; public static readonly DependencyProperty TitleProperty; public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
} static TitleTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TitleTextBox), new FrameworkPropertyMetadata(typeof(TitleTextBox))); TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(TitleTextBox), new UIPropertyMetadata(new PropertyChangedCallback(TitlePropertyChangedCallback)));
} private static void TitlePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TitleTextBox ttb = d as TitleTextBox; if (ttb._tbkTitle != null)
{
ttb._tbkTitle.Text = (string)e.NewValue;
}
} public override void OnApplyTemplate()
{
base.OnApplyTemplate(); _tbkTitle = Template.FindName(TitleTextBlockKey, this) as TextBlock;
_tbkTitle.Text = Title;
}
}
定义TitleTextBox样式,
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFTitleTextBox.Resources.Styles"
xmlns:uc="clr-namespace:WPFTitleTextBox">
<Style TargetType="{x:Type uc:TitleTextBox}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Height" Value="28"/>
<Setter Property="UndoLimit" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type uc:TitleTextBox}">
<Border x:Name="OuterBorder" BorderBrush="#8b99bc" BorderThickness="1" CornerRadius="1" Background="#f7f7f7">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions> <TextBlock x:Name="PART_TitleTextBlock" Text="{Binding Title}" Foreground="#a7abb0" VerticalAlignment="Center" Margin="5,0"/>
<Line Grid.Column="1" Stroke="#ccd1d7" StrokeDashArray="2,2" StrokeThickness="1.5" X1="0" Y1="0"
X2="0" Y2="{TemplateBinding Height}" Margin="0,4"/> <Border Grid.Column="2" Background="White">
<ScrollViewer x:Name="PART_ContentHost" Margin="5,0" VerticalAlignment="Center" FontSize="14"/>
</Border>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
在XAML中需要引用TitleTextBox。
<Grid>
<StackPanel>
<local:TitleTextBox Title="姓名" Width="270" Margin="5,10"/>
<local:TitleTextBox Title="班级" Width="270"/>
<local:TitleTextBox Title="专业" Width="270" Margin="5,10"/>
</StackPanel>
</Grid>
使用时设置一下Title即可。使用方式和普通TextBox一样。
以后如果遇到带Title的ComboBox,CheckBox等都可以参考上面的实现思路。
感谢您的阅读,代码点击这里下载。
WPF 实现带标题的TextBox的更多相关文章
- WPF中带水印的Textbox
很多时候我们都希望通过水印来告诉用户这里该填什么样格式的数据,那么我们就希望有这样的一个控件. 为了方便起见,先定义一个依赖属性专门来存放水印中显示的字符串. public sealed class ...
- WPF自学入门(六)WPF带标题的内容控件简单介绍
在WPF自学入门(二)WPF-XAML布局控件的文章中分别介绍StackPanel,WarpPanel,DockPanel,Grid,Canvas五种布局容器的使用,可以让我们大致了解容器可以使用在什 ...
- 带清空按钮TextBox的实现(WPF)
本博文针对人群:WPF新手.博文内容:通过Style制定包含清空Button的TextBox样式模板,通过在Style中引入自定义类的附加属性完成对TextBox的内容清空. <span sty ...
- WPF 实现带蒙版的 MessageBox 消息提示框
WPF 实现带蒙版的 MessageBox 消息提示框 WPF 实现带蒙版的 MessageBox 消息提示框 作者:WPFDevelopersOrg 原文链接: https://github.com ...
- Android自定义带标题边框的Layout
今天工作中又碰到个小问题,项目需要用到像Java Swing的JPanel一样带标题边框的布局,Android里没有类似控件,想到这个也不难,自己画了一个,是继承LinearLayout的一个自定义布 ...
- 基于jQuery带标题的图片3D切换焦点图
今天给大家分享一款基于jQuery带标题的图片3D切换焦点图.这款焦点图适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. 实现的代码. htm ...
- WPF 自带Datagrid编辑后无法更新数据源的问题
原文 WPF 自带Datagrid编辑后无法更新数据源的问题 解决办法: 在列的绑定属性里加上UpdateSourceTrigger,示例XAML如下 <DataGrid Grid.Row=& ...
- 带下拉列表的TextBox
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 示例:WPF实现ApplicationCommands.Delete的TextBox
原文:示例:WPF实现ApplicationCommands.Delete的TextBox 目的:通过模仿TextBox中Ctrl+C等快捷键原理来了解CommandBindings实现原理,可以通过 ...
随机推荐
- 【Other】Ubuntu 14.04 pptp 客户端连接配置
sudo apt-get install pptp-linux binutils modprobe ppp_mppe sudo pptpsetup --create testvpn --server ...
- .net MVC借助Iframe实现无刷新上传文件
html: <div id="uploadwindow" style="display: none;"> <form action=" ...
- 转帖: 使用脚本删除程序(免除在[控制面板]->[添加或删除程序]中的手工操作)
1. 代码:VBS strComputer = "." '这个表示本地计算机 Set objWMIService = GetObject("winmgmts:" ...
- sharepoint 2010 页面刷新时滚动条位置保持不变 Controlling scrollbar position on postback
sharepoint 2010 页面刷新时滚动条位置保持不变 Controlling scrollbar position on postback在sharepoint 2010中,如果当前页面的篇幅 ...
- poj 1028
http://poj.org/problem?id=1028 题意(水):做一个游览器的历史记录. back:后退到上一个页面,当上一个页面没有时,输出ignored. forward:向前一个页面, ...
- Qt Connect 信号 槽
信号和槽机制是 QT 的核心机制 .信号和槽是一种高级接口,应用于对象之间的通信,它是 QT 的核心特性,也是 QT 区别于其它工具包的重要地方.信号和槽是 QT 自行定义的一种通信机制,它独立于标准 ...
- Effective C++ -----条款39:明智而审慎地使用private继承
Private继承意味is-implemented-in-terms of(根据某物实现出).它通常比复合(composition)的级别低.但是当derived class需要访问protected ...
- 将项目导入eclipse中出现的jsp页面报错解决
- 使用json格式输出
/** * json输出 * * @param unknown_type $info */ public function json_out ($info) { header('Content-typ ...
- Mysql 数据库无法删除 41 错误
今天删除Mysql 数据库时候,没法删除,直接报错 41: 方法,进入 mysql的安装目录 我的是:D:\tools\Mysql\V76384-01\mysql-advanced-5.6.25-wi ...