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了一下,根据另一个兄台写的方法,拿来改造了一 ...
随机推荐
- mysql基础知识点
/* 启动MySQL */net start mysql /* 连接与断开服务器 */mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */mysqld ...
- 程序员的自我救赎---3.1:理解Oauth2.0
<前言> (一) Winner2.0 框架基础分析 (二)PLSQL报表系统 (三)SSO单点登录 (四) 短信中心与消息中心 (五)钱包系统 (六)GPU支付中心 (七)权限系统 (八) ...
- 通过邮箱发送html报表
前言 需求是发送邮件时, 可以将报表正文贴到邮件里, 可以正常复制选中报表内容. 目前的做法是简单粗暴的转成了一张图片, 这样效果显然是很糟糕的. 今天看到邮箱里可以预览Word, Excel, F1 ...
- Sencha Cmd 6 和 Ext JS 6 指南文档(部分官方文档中文翻译)
近期组织了几个程序员网友,正在翻译一部分官方的Sencha Cmd 6 和 Ext JS 6 指南文档. 眼下还没翻译完,大家能够先看看 Sencha Cmd 6 和 Ext JS 6 指南文档 ( ...
- Codeforces Round #313 (Div. 2) C
题目链接 题意: 有一个六边形,给你6条边的长度(顺时针给出).每条边都是整数,问你它能够被切割成几个单位长度的正三角形 (题目保证给出的数据能够被切割) 思路: 六边形能够被切割成两种情况: ① ...
- [Python学习] 简单网络爬虫抓取博客文章及思想介绍
前面一直强调Python运用到网络爬虫方面很有效,这篇文章也是结合学习的Python视频知识及我研究生数据挖掘方向的知识.从而简介下Python是怎样爬去网络数据的,文章知识很easy ...
- Maven依赖的是本地工程还是仓库jar包?
相信大家都碰见过maven配置的依赖或者是jar包或者是工程,在开发的过程当中,我们当然需要引入的是工程,这样查看maven依赖的文件的时候,就能直接查看到源码. 一.本地工程依赖 举个例子,其架构如 ...
- Asp.net mvc 知多少(二)
本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问http:/ ...
- 学习Spring必学的Java基础知识(1)----反射(转)
引述要学习Spring框架的技术内幕,必须事先掌握一些基本的Java知识,正所谓"登高必自卑,涉远必自迩".以下几项Java知识和Spring框架息息相关,不可不学(我将通过一个系 ...
- 自学Python2.1-基本数据类型-字符串str(object)
Python str方法总结 class str(object): """ str(object='') -> str str(bytes_or_buffer[, ...