当前位置:首页 > Windows程序 > 正文

new FrameworkPropertyMetadata(( object ) null

2024-03-31 Windows程序

问题:自界说控件的依赖项属性和VIewModel中的变量不能双向绑定

解决思路:比拟.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); }

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/35106.html

Jm-杰米博客Jamie
草根站长的技术交流乐园!IT不会不要紧快来好好学习吧!
  • 20786文章总数
  • 7494590访问次数
  • 建站天数
  • 友情链接