DependencyProperty does not work if the value is from a binding

后端 未结 2 1131
一向
一向 2021-01-29 14:37

I created UserControl with viewmodel. It has DependencyProperty which only works if the value is passed directly. If the value is passed through the binding, it no longer works.

2条回答
  •  一整个雨季
    2021-01-29 14:52

    There is no IsConnected property in the SomeViewModel instance in the current DataContext of the UserControl, hence the Binding

    
    

    won't work. It tries to resolve the PropertyPath against the current DataContext, unless you explicitly specify its Source, RelativeSource or ElementName.

    This is the exact reason why UserControls should never explicitly set their own DataContext, and hence never have something like an own, private view model.

    The elements in the UserControl's XAML would not bind to properties of such a private view model object, but directly to the properties of the UserControl, for example like

    
    

提交回复
热议问题