WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果
来源:互联网 发布:悟空传百年孤寂知乎 编辑:程序博客网 时间:2024/06/15 16:07
本以为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 = elementas 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 事件效果
- TextBox如何触发TextChanged事件
- wpf loaded 和TargetUpdated事件区别
- RichTextBox.Rtf 属性设置居然会触发两次TextChanged事件
- WPF 实现跑马灯效果的Label控件,数据绑定方式实现
- 在WPF中绑定类的属性至Label控件实现自动更新显示
- 触发textbox的TextChanged事件有感
- DataGridView数据绑定控件---------按钮添加和事件触发总结
- DataGridView数据绑定控件---------按钮添加和事件触发总结
- DataGridView数据绑定控件---------按钮添加和事件触发总结
- WPF MVVM后台绑定没有Command属性的控件,绑定任何事件事件
- 【WPF】实现按钮点击触发事件
- RadioButtonList绑定后台数据,触发点击事件
- wpf触发点击事件
- 关于页面多个文本框触发textChanged事件的问题
- C#:ComboBox中TextChanged事件不被触发的探究
- C#:ComboBox中TextChanged事件不被触发的探究
- WPF 在事件中绑定命令
- 插入排序
- Java简单实现MD5加密
- 分布式通信协议
- 第11讲项目5-坐标转换
- HDU 1754 I Hate It(线段树)
- WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果
- 基于vs2015 64位 boost python库的生成
- 哈希(HASH)冲突的处理方法
- java堆比例设置
- memcache分布式部署的原理分析
- python笔记
- 二分法递归表示(java基本算法)
- 洛谷p1156垃圾陷阱
- 我在清华当工程师的日子