WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果
原文:WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果
本以为Label也有TextChanged
事件,但在使用的时候却没找到,网友说Label的Content属性改变肯定是使用赋值操作,赋值的时候就可以对其进行相应的操作所以不需TextChanged
事件。
上MSDN查了一下,TextChanged
事件在TextBoxBase类中;而Label和TextBox的继承关系如下:
Label:
System.Object
System.Windows.Threading.DispatcherObject
System.Windows.DependencyObject
System.Windows.Media.Visual
System.Windows.UIElement
System.Windows.FrameworkElement
System.Windows.Controls.Control
System.Windows.Controls.ContentControl
System.Windows.Controls.Label
TextBox:
System.Object
System.Windows.Threading.DispatcherObject
System.Windows.DependencyObject
System.Windows.Media.Visual
System.Windows.UIElement
System.Windows.FrameworkElement
System.Windows.Controls.Control
System.Windows.Controls.Primitives.TextBoxBase
System.Windows.Controls.TextBox
System.Windows.Controls.Primitives.DatePickerTextBox
System.Windows.Controls.Ribbon.RibbonTextBox
从上面红色就可以看出继承路径的不同,所以Label没有TextChanged
事件;
如何实现修改Label的Content属性自动执行类似TextChanged 事件呢?
这里实现了一种使用数据绑定的方式,借助TargetUpdated事件进行类似TextChanged
事件;
具体代码供参考:
项目中使用工厂模式设置好了数据绑定:
if (element
is UIElement)
{
UIElement uiElement = element
as UIElement;
Binding binding =
new Binding();
binding.Mode =
BindingMode.TwoWay;
binding.Path = new PropertyPath(item.Value.DataPath);
binding.Source = item.Value.DataSource;
binding.NotifyOnTargetUpdated = true;
DependencyProperty dependProperty = GetDependencyProperty(item.Value.BindingProperty);
BindingOperations.SetBinding(uiElement, dependProperty, binding);
}
想使用TargetUpdated事件binding.NotifyOnTargetUpdated
属性必须为True;
然后为Label注册 TargetUpdated 事件:
label.TargetUpdated += Label_DataContextChanged;
至此就可以简单实现类似TextChanged
事件。
不对之处望指教!
WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果的更多相关文章
- WPF 实现跑马灯效果的Label控件,数据绑定方式实现
原文:WPF 实现跑马灯效果的Label控件,数据绑定方式实现 项目中需要使用数据绑定的方式实现跑马灯效果的Label,故重构了Label控件:具体代码如下 using System; using S ...
- WPF 用户控件的自定义依赖属性在 MVVM 模式下的使用备忘
依赖属性相当于扩充了 WPF 标签的原有属性列表,并可以使用 WPF 的绑定功能,可谓是十分方便的:用户控件则相当于代码重用的一种方式:以上几点分开来还是比较好理解的,不过要用到MVVM 模式中,还是 ...
- WPF系列 —— 控件添加依赖属性(转)
WPF系列 —— 控件添加依赖属性 依赖属性的概念,用途 ,如何新建与使用.本文用做一个自定义TimePicker控件来演示WPF的依赖属性的简单应用. 先上TimePicker的一个效果图. 概念 ...
- WPF布局控件与子控件的HorizontalAlignment/VerticalAlignment属性之间的关系
WPF布局控件与子控件的HorizontalAlignment/VerticalAlignment属性之间的关系: 1.Canvas/WrapPanel控件: 其子控件的HorizontalAlign ...
- wpf控件开发基础(3) -属性系统(2)
原文:wpf控件开发基础(3) -属性系统(2) 上篇说明了属性存在的一系列问题. 属性默认值,可以保证属性的有效性. 属性验证有效性,可以对输入的属性进行校验 属性强制回调, 即不管属性有无发生变化 ...
- wpf控件开发基础(4) -属性系统(3)
原文:wpf控件开发基础(4) -属性系统(3) 知识回顾 接上篇,上篇我们真正接触到了依赖属性的用法,以及依赖属性的属性元数据的用法,并且也实实在在地解决了之前第二篇提到的一系列问题.来回顾一下 属 ...
- wpf控件开发基础(2) -属性系统(1)
原文:wpf控件开发基础(2) -属性系统(1) 距离上篇写的时间有1年多了.wpf太大,写的东西实在太多,我将依然围绕着自定义控件来展开与其相关的技术点. 也欢迎大家参与讨论.这篇我们将要讨论的是W ...
- WPF常用控件应用demo
WPF常用控件应用demo 一.Demo 1.Demo截图如下: 2.demo实现过程 总体布局:因放大缩小窗体,控件很根据空间是否足够改变布局,故用WrapPanel布局. <ScrollVi ...
- WPF 分页控件 WPF 多线程 BackgroundWorker
WPF 分页控件 WPF 多线程 BackgroundWorker 大家好,好久没有发表一篇像样的博客了,最近的开发实在头疼,很多东西无从下口,需求没完没了,更要命的是公司的开发从来不走正规流程啊, ...
随机推荐
- Linux下kill进程脚本
Linux下kill进程脚本 在Linux有时会遇到需要kill同一个程序的进程,然而这个程序有多个进程,一一列举很是繁琐,使用按名字检索,统一kill Perl脚本 使用方法 kill_all.pl ...
- [Angular] Providers and useFactory
// service.ts import { Injectable, Inject } from '@angular/core'; import { Http } from '@angular/htt ...
- [Angular2] Map keyboards events to Function
The idea is when we tape the arrow keys on the keyboard, we want the ball move accodingly. const lef ...
- ENVI显示GDAL创建GeoTiff文件的一个问题及其思考
作者:朱金灿 来源:http://blog.csdn.net/clever101 使用gdal创建一个100*100的红色的geotiff图像,代码如下: #include <assert.h& ...
- 【HDU5748】Bellovin
Description Peter has a sequence and he define a function on the sequence -- , where is the length ...
- SpringMVC中跳转路径的问题
1. @RequestMapping 1)@RequestMapping既可以作用于类,也可以作用于方法 2)@RequestMapping中value值(即跳转的路径),可以加 "/&qu ...
- 关于用WebView或手机浏览器打开连接问题
1.通常情况下 大家可能都想使用WebView打开网页内部链接而不想再调用手机浏览器,我们可以通过以下两种方法实现: (1)为WebView设置一个WebViewClient,并重写shouldOve ...
- [React] displayName for stateless component
We can use 'displayName' on component to change its component tag in dev tool: import React from 're ...
- KeePass v1.30
Changes from 1.29 to 1.30: New Features:Refined application icons (thanks to Victor Andreyenkov).Add ...
- SpringBoot JPA 专题
Error: Error starting ApplicationContext. To display the auto-configuration report re-run your appli ...