这篇博客将分享在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. (转) C#如何使用异步编程

    怎么使用异步,就是用委托进行处理,如果委托对象在调用列表中只有一个方法,它就可以异步执行这个方法.委托类有两个方法,叫做BeginInvoke和EndInvoke,它们是用来异步执行使用. 异步有三种 ...

  2. 在中文windows下使用pywinauto进行窗口操作

    这两天开始接触pywinauto,听说百度的自动化QA也用这个模块,于是来了兴趣,但网上的教程很少,而且基本上都是拿官方的notepad来说,首先中文菜单的支持是问题,其次各种操作也没有写清楚,阅读官 ...

  3. spring boot redis缓存JedisPool使用

    spring boot redis缓存JedisPool使用 添加依赖pom.xml中添加如下依赖 <!-- Spring Boot Redis --> <dependency> ...

  4. Unity3d《Shader篇》地球旋转上空悬浮云层

    记得把纹理设置成Repeat Shader "Custom/Earth" { Properties { _MainTex ("Base (RGB)", 2D) ...

  5. Mathematics:Prime Path(POJ 3126)

    素数通道 题目大意:给定两个素数a,b,要你找到一种变换,使得每次变换都是素数,如果能从a变换到b,则输出最小步数,否则输出Impossible 水题,因为要求最小步数,所以我们只需要找到到每个素数的 ...

  6. VB兼容问题

    window7 64位无法显示打印窗问题 在Windows7 64位和VS2008环境下,PrintDialog.ShowDialog不能显示打印对话框 在VS2008中编写?如下代码: PrintD ...

  7. 数据结构顺序表删除所有特定元素x

    顺序表类定义: template<class T> class SeqList : { public: SeqList(int mSize); ~SeqList() { delete[] ...

  8. 【leetcode】Wildcard Matching(hard) ★ 大神太牛了

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  9. MyEclipse在线安装maven插件最新地址

    http://repository.sonatype.org/content/sites/forge-sites/m2e/0.10.0/S/20100209-0800/

  10. Java异常题库

    一.填空题 __异常处理__机制是一种非常有用的辅助性程序设计方法.采用这种方法可以使得在程序设计时将程序的正常流程与错误处理分开,有利于代码的编写和维护. 在Java异常处理中可以使用多个catch ...