问题:自界说控件的依赖项属性和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); }
,