DependencyProperty does not work if the value is from a binding

后端 未结 2 1130
一向
一向 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:53

    When you set the DataContext explicitly in the UserControl like this:

    
        
    
    

    ...you can no longer bind to SomeView's DataContext in the consuming view like this:

    
    

    ...because SomeViewModel doesn't have any IsConnected property.

    You should avoid setting the DataContext explicitly and let the UserControl inherit its DataContext from its parent element. You can still bind to the dependency property of the UserControl itself using a RelativeSource or an ElementName:

    
        
            
        
    
    

    Besides, SomeViewModel seems superfluous in your example since the UserControl already has an Active property.

提交回复
热议问题