这篇博客将分享在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的更多相关文章

  1. WPF中带水印的Textbox

    很多时候我们都希望通过水印来告诉用户这里该填什么样格式的数据,那么我们就希望有这样的一个控件. 为了方便起见,先定义一个依赖属性专门来存放水印中显示的字符串. public sealed class ...

  2. WPF自学入门(六)WPF带标题的内容控件简单介绍

    在WPF自学入门(二)WPF-XAML布局控件的文章中分别介绍StackPanel,WarpPanel,DockPanel,Grid,Canvas五种布局容器的使用,可以让我们大致了解容器可以使用在什 ...

  3. 带清空按钮TextBox的实现(WPF)

    本博文针对人群:WPF新手.博文内容:通过Style制定包含清空Button的TextBox样式模板,通过在Style中引入自定义类的附加属性完成对TextBox的内容清空. <span sty ...

  4. WPF 实现带蒙版的 MessageBox 消息提示框

    WPF 实现带蒙版的 MessageBox 消息提示框 WPF 实现带蒙版的 MessageBox 消息提示框 作者:WPFDevelopersOrg 原文链接: https://github.com ...

  5. Android自定义带标题边框的Layout

    今天工作中又碰到个小问题,项目需要用到像Java Swing的JPanel一样带标题边框的布局,Android里没有类似控件,想到这个也不难,自己画了一个,是继承LinearLayout的一个自定义布 ...

  6. 基于jQuery带标题的图片3D切换焦点图

    今天给大家分享一款基于jQuery带标题的图片3D切换焦点图.这款焦点图适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. 实现的代码. htm ...

  7. WPF 自带Datagrid编辑后无法更新数据源的问题

    原文  WPF 自带Datagrid编辑后无法更新数据源的问题 解决办法: 在列的绑定属性里加上UpdateSourceTrigger,示例XAML如下 <DataGrid Grid.Row=& ...

  8. 带下拉列表的TextBox

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 示例:WPF实现ApplicationCommands.Delete的TextBox

    原文:示例:WPF实现ApplicationCommands.Delete的TextBox 目的:通过模仿TextBox中Ctrl+C等快捷键原理来了解CommandBindings实现原理,可以通过 ...

随机推荐

  1. Docker内部存储结构(devicemapper)解析(续)

    dm.fs 参数dm.fs可以指定容器的rootfs的文件系统,但只支持ext4/xfs: func NewDeviceSet(root string, doInit bool, options [] ...

  2. 阿里云ecs云服务器安装wdcp控制面板教程

    以前就听说服务器非常的难,而且我也不懂代码,不懂英文,我怕自己学不会就买了一个月的.开始我都不知道啥样的服务器,还是我的一位哥们给我远程买的,他说这个镜像最稳定了. 服务器买好后我便开始研究,可是怎么 ...

  3. Course Schedule I & II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  4. Wince下sqlce数据库开发(二)

    上次写到使用数据绑定的方法测试本地sqlce数据库,这次使用访问SQL Server的方法访问sqlce,你会发现他们是如此的相似... 参考资料:http://www.cnblogs.com/rai ...

  5. POJ 2559

    http://poj.org/problem?id=2559 题意:就是找出可以完整连接的最大的矩形面积. 思路:找出单独的一块矩形,往两边延伸,记录两边的比他高的矩形是在哪个位置,然后最右的位置减去 ...

  6. 压测 apache ab 初探

    2015年10月30日 14:58:34 ab是apache自带的压测命令, 在其bin目录下边, 不仅可以压测Apache, 也可以测nginx或其他服务器 可以模拟上传post值 (-p, 与下边 ...

  7. ACM/ICPC 之 Bellman Ford练习题(ZOJ1791(POJ1613))

    这道题稍复杂一些,需要掌握字符串输入的处理+限制了可以行走的时间. ZOJ1791(POJ1613)-Cave Raider //限制行走时间的最短路 //POJ1613-ZOJ1791 //Time ...

  8. ffmpeg-20160510-git-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...

  9. pthread_cond_wait的原子性

    使用的基本模板如下(参考APUE): signal代码序列如下, pthread_mutex_lock ... pthread_cond_signal pthread_mutex_unlock   w ...

  10. 2106 Problem F Shuffling Along 中石油-未提交-->已提交

    题目描述 Most of you have played card games (and if you haven’t, why not???) in which the deck of cards ...