wpf Textbox 点击选中全部文本
用法:依赖属性
SelectTextOnFocus.Active = True
public class SelectTextOnFocus : DependencyObject
{
public static readonly DependencyProperty ActiveProperty = DependencyProperty.RegisterAttached(
"Active",
typeof(bool),
typeof(SelectTextOnFocus),
new PropertyMetadata(false, ActivePropertyChanged)); private static void ActivePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TextBox)
{
TextBox textBox = d as TextBox;
if ((e.NewValue as bool?).GetValueOrDefault(false))
{
textBox.GotKeyboardFocus += OnKeyboardFocusSelectText;
textBox.PreviewMouseLeftButtonDown += OnMouseLeftButtonDown;
}
else
{
textBox.GotKeyboardFocus -= OnKeyboardFocusSelectText;
textBox.PreviewMouseLeftButtonDown -= OnMouseLeftButtonDown;
}
}
} private static void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DependencyObject dependencyObject = GetParentFromVisualTree(e.OriginalSource); if (dependencyObject == null)
{
return;
} var textBox = (TextBox)dependencyObject;
if (!textBox.IsKeyboardFocusWithin)
{
textBox.Focus();
e.Handled = true;
}
} private static DependencyObject GetParentFromVisualTree(object source)
{
DependencyObject parent = source as UIElement;
while (parent != null && !(parent is TextBox))
{
parent = VisualTreeHelper.GetParent(parent);
} return parent;
} private static void OnKeyboardFocusSelectText(object sender, KeyboardFocusChangedEventArgs e)
{
TextBox textBox = e.OriginalSource as TextBox;
if (textBox != null)
{
textBox.SelectAll();
}
} [AttachedPropertyBrowsableForChildrenAttribute(IncludeDescendants = false)]
[AttachedPropertyBrowsableForType(typeof(TextBox))]
public static bool GetActive(DependencyObject @object)
{
return (bool) @object.GetValue(ActiveProperty);
} public static void SetActive(DependencyObject @object, bool value)
{
@object.SetValue(ActiveProperty, value);
}
}
wpf Textbox 点击选中全部文本的更多相关文章
- WPF TextBox 获得焦点后,文本框中的文字全选中
textbox.GotFocus 事件处理 Textbox.SelectAll() 是不行的, 这样处理会发生的情况是: 1) textbox1 当前没有焦点, 内容为 someText. 2) 鼠标 ...
- 如何在Word中批量选中特定文本
如何在Word中批量选中特定文本 举个例子,我们对如下文本进行操作,将文本中所有的“1111111”标红,所有的“2222222”标绿,所有的“3333333”标蓝 在Word中找到“查找”下的“高级 ...
- javascript获取选中的文本/html
首先来谈一下Selection对象和Range对象. Selection是window.getSelection()方法返回的一个对象,用于表示用户选中的文本区域.Selection对象表现为一组Ra ...
- [WPF]ListView点击列头排序功能实现
[转] [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...
- C# 全选中数字文本框内容
/// <summary> /// 全选中数字文本框内容 /// </summary> /// <param name=&quo ...
- jquery获取选中的文本和值
jquery获取选中的文本和值 1.说明 (1)获取select下拉框选中的索引 $("#selection").get(0).selectedIndex; (2)获取 ...
- OpenLayers学习笔记(三)— QML与HTML通信之 地图上点击添加自由文本
实现在地图随意点击,弹出文本输入框,输入任意文字,完成自由文本添加的功能 作者: 狐狸家的鱼 GitHub:八至 本文链接:地图上点击添加自由文本 关于如何QML与HTML通信已经在上一篇文章 QML ...
- phpcms栏目点击选中
点击选中(没有二级栏目) {pc:content action="category" catid="0" num="4" siteid=&q ...
- PS-点击选中某一个图层
需要点击选中某一图层的时候,需要勾选[自动选择]
随机推荐
- 导入不同业务数据通过Excel实现
很多公司都用到了老系统移植到新系统,数据自然也需要迁移,这个解决方案之一就是使用Excel文件导入. 结合公司实现,然后简单写了个Demo. (PS:去找朋友本想着花几十分钟弄出来炫耀一波,结果花了三 ...
- Oracle 关于oracle自带的行转列函数
前言: 环境是java+hibernate+oracle11g 目标是将某表中根据id分组后将name字段的值拼接到一列中,且用“,“进行分割 试过用 wm_concat() 结合 group by ...
- JNDI连接池连接Oracle数据库
今天做了一个评论的小功能,要求用JNDI连接池连接Oracle数据库,以前只是测试了是否连接的上,现在没想到一个JNDI连接池连接Oracle数据库,纠结了好久,原来都是Oracle数据库的问题,这是 ...
- 读书笔记「Python编程:从入门到实践」_5.if语句
5.1 一个简单示例 cars = ['audi', 'bmw', 'subaru', 'toyota'] for car in cars: if car == 'bmw': print(car.up ...
- 为什么使用dispatch_sync
1.充分利用多线程的性能: 2.将分散在多线程中的核心操作归并到一个queue执行(通常为一个串行队列). 3.便于在任务线程中进行后继操作. 术语: 任务线程: 同步模块.
- PyCharm与GitHub环境配置
转载地址:https://blog.csdn.net/xierhacker/article/details/70053162 一.准备工作 Ⅰ.git下载和安装 要连接GitHub,首先git是必不可 ...
- PS通道的界面颜色设置
编辑--首选项---界面--界面---选项---(勾选)以彩色显示通道(彩色显示)或者不勾选(为黑白色显示)
- BZOJ 4327: JSOI2012 玄武密码 后缀自动机
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...
- Got permission denied while trying to connect to the Docker daemon socket at unix
拉取Dockerimages时错误信息如下: [master@localhost ~]$ docker pull redis Using default tag: latest Got permiss ...
- vue 强制刷新组件
<component v-if="hackReset"></component> 2 3 4 this.hackReset = false this.$ne ...