[WPF源码分析]ContentControl依赖项属性的双向绑定,two-way binding view's DependencyProperty and ViewModel's variable
2018-01-24 17:40
591 查看
问题:自定义控件的依赖项属性和VIewModel中的变量不能双向绑定
解决思路:对比.net源码 PresentationFramework / System.Windows.Controls
原因:定义依赖项属性时没有设置OnChanged方法
解决方法:初始化时绑定Changed方法
.net 源码如下:
解决思路:对比.net源码 PresentationFramework / System.Windows.Controls
原因:定义依赖项属性时没有设置OnChanged方法
解决方法:初始化时绑定Changed方法
.net 源码如下:
/// <summary> /// The DependencyProperty for the Content property. /// Flags: None /// Default Value: null /// </summary> [CommonDependencyProperty] public static readonly DependencyProperty ContentProperty = DependencyProperty.Register( "Content", typeof(object), typeof(ContentControl), new FrameworkPropertyMetadata( (object)null, new PropertyChangedCallback(OnContentChanged))); /// <summary> /// Content is the data used to generate the child elements of this control. /// </summary> [Bindable(true), CustomCategory("Content")] public object Content { get { return GetValue(ContentProperty); } set { SetValue(ContentProperty, value); } } /// <summary> /// Called when ContentProperty is invalidated on "d." /// </summary> private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { // 根据需要实现自己的方法 ContentControl ctrl = (ContentControl) d; ctrl.SetValue(HasContentPropertyKey, (e.NewValue != null) ? BooleanBoxes.TrueBox : BooleanBoxes.FalseBox); ctrl.OnContentChanged(e.OldValue, e.NewValue); }
相关文章推荐
- Vue 2.3.4源码分析之双向绑定原理
- AngularJS双向数据绑定的源码分析
- 通过源码分析Vue的双向数据绑定详解
- 双向绑定---angular之watch、apply、digest原理深入分析(源码分析)
- React Mixin 双向绑定 及 源码分析
- vue双向绑定的原理及实现双向绑定MVVM源码分析
- WPF技巧(2)绑定到附加属性
- [置顶] laravel 框架 三种绑定 bind、singleton、instance 源码分析
- asp.net mvc源码分析-BeginForm方法 和ClientValidationEnabled 属性
- jQuery-1.9.1源码分析系列(八) 属性操作
- WPF DataGrid中列的属性绑定问题
- 【WPF】如何把一个枚举属性绑定到多个RadioButton
- C#使用Xamarin开发可移植移动应用(4.进阶篇MVVM双向绑定和命令绑定)附源码
- Netty3 源码分析 - NIO server绑定过程分析
- Vue学习之源码分析--从Vue.js源码角度再看数据绑定(三)
- WPF中数据绑定的Validation应用分析
- jQuery two way bindings(双向数据绑定插件)
- Angular 2 自定义 双向绑定 属性
- jQuery源码分析--event事件绑定(下)
- WPF:依赖属性的数据绑定