WPF下可编辑Header的Tab控件实现
介绍
有这样一个需求,当用户双击Tab控件Header区域时, 希望可以直接编辑。对于WPF控件,提供一个ControlTemplate在加上一些Trigger就可以实现。效果如下:
代码
首先,我们需要给Tab Header设计一个ControlTemplate。类似一个TextBlock,双击进入编辑状态。 所以Xaml如下:
< Setter Property="Template"> < Setter.Value > < ControlTemplate TargetType="{x:Type local:EditableTabHeaderControl}"> < Grid > < TextBox x:Name="PART_TabHeader" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content, Mode=TwoWay}" Visibility="Collapsed"/> < TextBlock x:Name="PART_TextBlock" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content, Mode=TwoWay}"/> </ Grid > < ControlTemplate.Triggers > < Trigger Property="IsInEditMode" Value="True"> < Trigger.Setters > < Setter TargetName="PART_TabHeader" Property="Visibility" Value="Visible"/> < Setter TargetName="PART_TextBlock" Property="Visibility" Value="Collapsed"/> </ Trigger.Setters > </ Trigger > </ ControlTemplate.Triggers > </ ControlTemplate > </ Setter.Value > </ Setter > |
接下来,我们需要定义个“EditableTabHeaderControl”类,它具有控制TextBox和TextBlock的能力。如下:
namespace EditableTabHeaderDemo { using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Threading; /// <summary> /// Header Editable TabItem /// </summary> [TemplatePart(Name = "PART_TabHeader" , Type = typeof (TextBox))] public class EditableTabHeaderControl : ContentControl { /// <summary> /// Dependency property to bind EditMode with XAML Trigger /// </summary> private static readonly DependencyProperty IsInEditModeProperty = DependencyProperty.Register( "IsInEditMode" , typeof ( bool ), typeof (EditableTabHeaderControl)); private TextBox textBox; private string oldText; private DispatcherTimer timer; private delegate void FocusTextBox(); /// <summary> /// Gets or sets a value indicating whether this instance is in edit mode. /// </summary> public bool IsInEditMode { get { return ( bool ) this .GetValue(IsInEditModeProperty); } set { if ( string .IsNullOrEmpty( this .textBox.Text)) { this .textBox.Text = this .oldText; } this .oldText = this .textBox.Text; this .SetValue(IsInEditModeProperty, value); } } /// <summary> /// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate"/>. /// </summary> public override void OnApplyTemplate() { base .OnApplyTemplate(); this .textBox = this .Template.FindName( "PART_TabHeader" , this ) as TextBox; if ( this .textBox != null ) { this .timer = new DispatcherTimer(); this .timer.Tick += TimerTick; this .timer.Interval = TimeSpan.FromMilliseconds(1); this .LostFocus += TextBoxLostFocus; this .textBox.KeyDown += TextBoxKeyDown; this .MouseDoubleClick += EditableTabHeaderControlMouseDoubleClick; } } /// <summary> /// Sets the IsInEdit mode. /// </summary> /// <param name="value">if set to <c>true</c> [value].</param> public void SetEditMode( bool value) { this .IsInEditMode = value; this .timer.Start(); } private void TimerTick( object sender, EventArgs e) { this .timer.Stop(); this .MoveTextBoxInFocus(); } private void MoveTextBoxInFocus() { if ( this .textBox.CheckAccess()) { if (! string .IsNullOrEmpty( this .textBox.Text)) { this .textBox.CaretIndex = 0; this .textBox.Focus(); } } else { this .textBox.Dispatcher.BeginInvoke(DispatcherPriority.Render, new FocusTextBox( this .MoveTextBoxInFocus)); } } private void TextBoxKeyDown( object sender, KeyEventArgs e) { if (e.Key == Key.Escape) { this .textBox.Text = oldText; this .IsInEditMode = false ; } else if (e.Key == Key.Enter) { this .IsInEditMode = false ; } } private void TextBoxLostFocus( object sender, RoutedEventArgs e) { this .IsInEditMode = false ; } private void EditableTabHeaderControlMouseDoubleClick( object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { this .SetEditMode( true ); } } } } |
这里有一个问题,当控件进入编辑状态,TextBox变为可见状态时,它不能自动获得focus。一种解决办法是挂一个Timer,每1毫秒轮询一次,检查状态并控制focus。
现在就来添加一个WPF TabControl,并应用ItemContainerStyle。然后双击Header,可以编辑啦~
< Window x:Class="EditableTabHeaderDemo.MainWindow" xmlns:local="clr-namespace:EditableTabHeaderDemo" Title="EditableTabHeaderDemo" Height="300" Width="500"> < Window.Resources > < Style x:Key="EditableTabHeaderControl" TargetType="{x:Type local:EditableTabHeaderControl}"> <!-- The template specified earlier will come here !--> </ Style > < Style x:Key="ItemContainerStyle" TargetType="TabItem"> < Setter Property="HeaderTemplate"> < Setter.Value > < DataTemplate > < local:EditableTabHeaderControl Style="{StaticResource EditableTabHeaderControl}"> < local:EditableTabHeaderControl.Content > < Binding Path="Name" Mode="TwoWay"/> </ local:EditableTabHeaderControl.Content > </ local:EditableTabHeaderControl > </ DataTemplate > </ Setter.Value > </ Setter > </ Style > < DataTemplate x:Key="ContentTemplate"> < Grid > < TextBlock HorizontalAlignment="Left" Text="{Binding Name}"/> < TextBlock HorizontalAlignment="Center" Text="{Binding City}"/> </ Grid > </ DataTemplate > </ Window.Resources > < Grid > < TabControl Grid.Row="0" ItemsSource="{Binding Data}" ItemContainerStyle="{StaticResource ItemContainerStyle}" ContentTemplate="{StaticResource ContentTemplate}" /> </ Grid > </ Window > |
开发工具
ComponentOne Studio WPF 是专为桌面应用程序开发所准备的一整套控件包,崇尚优雅和创新,以“触控优先”为设计理念,内含轻量级高性能表格控件,和大量类型丰富的2D和3D图表控件,能使开发的应用程序更富创意。
许可证
本文以及示例代码文件遵循The Code Project Open License(CPOL)。
源码下载
英文链接:Header Editable Tab Control in Wpf
WPF下可编辑Header的Tab控件实现的更多相关文章
- WPF 程序如何移动焦点到其他控件
原文:WPF 程序如何移动焦点到其他控件 WPF 中可以使用 UIElement.Focus() 将焦点设置到某个特定的控件,也可以使用 TraversalRequest 仅仅移动焦点.本文介绍如何在 ...
- 《Programming WPF》翻译 第5章 7.控件模板
原文:<Programming WPF>翻译 第5章 7.控件模板 如果仔细的看我们当前的TTT游戏,会发现Button对象并没有完全为我们工作.哪些TTT面板有内圆角? 图5-14 这里 ...
- WPF从我炫系列4---装饰控件的用法
这一节的讲解中,我将为大家介绍WPF装饰控件的用法,主要为大家讲解一下几个控件的用法. ScrollViewer滚动条控件 Border边框控件 ViewBox自由缩放控件 1. ScrollView ...
- WPF自定义控件(二)の重写原生控件样式模板
话外篇: 要写一个圆形控件,用Clip,重写模板,去除样式引用圆形图片可以有这三种方式. 开发过程中,我们有时候用WPF原生的控件就能实现自己的需求,但是样式.风格并不能满足我们的需求,那么我们该怎么 ...
- WPF编程,通过KeyFrame 类型制作控件线性动画的一种方法。
原文:WPF编程,通过KeyFrame 类型制作控件线性动画的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/articl ...
- WPF教程002 - 实现Step步骤条控件
原文:WPF教程002 - 实现Step步骤条控件 在网上看到这么一个效果,刚好在用WPF做控件,就想着用WPF来实现一下 1.实现原理 1.1.该控件分为2个模块,类似ComboBox控件分为Ste ...
- WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探
原文:WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探 最近因为项目需要,开始学习如何使用WPF开发桌面程序.使用WPF一段时间之后,感 ...
- [工具推荐]005.Axure RP Pro 7.0模拟C#TAB控件
有一次,主管安排我写一个项目的原型,但是项目中涉及到了Tab控件,在Axure中的控件中找了一番,没有找着Tab控件.那么我们只能换种法子来实现它了,我们用到了Dynamic Panel来模拟. 1. ...
- 扩展easyUI tab控件,添加加载遮罩效果
项目里要用HighChart显示图表,如果返回的数量量太多,生成图表是一个很耗时的过程.tab控件又没有显示遮罩的设置(至少本菜是没有找到), Google了一下,根据另一个兄台写的方法,拿来改造了一 ...
随机推荐
- redis秒杀
用Redis轻松实现秒杀系统 秒杀系统的架构设计 秒杀系统,是典型的短时大量突发访问类问题.对这类问题,有三种优化性能的思路: 写入内存而不是写入硬盘 异步处理而不是同步处理 分布式处理 用上这三招, ...
- PHP如何强制下载文件
很多网站都需要做文件下载的功能.如果直接给连接的方式下载的话有很多的弊处...因为有时候需要对下载权限的检查,对下载次数的检查.所以一般采用php的方法进行安全下载.但是下载的时候如果是txt jpg ...
- webpack 图片的打包
1. 在img文件夹下随便找一个小一点的图片放进去. 2.修改entry.js require('../css/index.css'); var demo1 = require('../js/demo ...
- 实现一个websocket常驻进程服务
由于工作的原因,近期调查了一下mac系统下常驻服务的接收websocket信息和创建进程的方法原理.将具体实现细节记录下来备忘. (一).准备工作 1.安装brew,在终端中输入: ruby -e & ...
- MySql的隔离级别和锁的关系
一.事务的4个基本特征 Atomic(原子性): 事务中包括的操作被看做一个逻辑单元.这个逻辑单元中的操作要 么所有成功.要么所有失败. Consistency(一致性): 仅仅有合法的数据能 ...
- 查看主机DNSserver
一.Nslookup(name server lookup)( 域名查询):是一个用于查询 Internet 域名信息或诊断DNS server问题的工具.nslookup能够指定查询的类型,能够查到 ...
- 第一安装oracle数据库后,需要创建一个用户,给用户解锁并赋予权限
1.第一次安装oracle数据库应该做的事情. 注: 1.安装oracle后需要创建用户,连接数据库,(注意数据库名,还有好像后面的 ":"也有影响) 2.解锁用户, 3.授予新登 ...
- 关于postgres中的一些宏的tips
Postgresql作为C语言开发的代码,其中大量的运用了一些宏的操作. 因此理解这些宏很重要,然而有时候这些宏总让人很费解. 作为一个经常翻翻postgresql源码的小白,在这里做一个记录吧,方便 ...
- jmeter+ant+jenkins的自动化接口测试
一.Jenkins安装配置 1.安装配置JDK1.7+环境变量: 2.下载jenkins.war,放入D:\jenkins目录下,目录位置随意: Jenkins启动方法: cmd进入Jenkins目录 ...
- 【CSS3】渐变
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...