WPF binding multiple controls to different datacontexts

后端 未结 4 2002
不知归路
不知归路 2020-12-12 21:21

I have a scenario where I don\'t really know how to bind data to controls hosted in a UserControl to multiple datacontexts.

The data i want to bind comes from 2 clas

4条回答
  •  情歌与酒
    2020-12-12 22:23

    Both Rich and bendewey had good answers. Exploring this same topic today in Silverlight instead of WPF, I found that it's not necessary to establish multiple DataContexts. Revising bendewey's example:

    
      
           
           
           
      
    
    

    Using the Binding Path you gain the flexibility to mix and match bindings to properties of different classes without concern for the DataContext of the controls' containers.

    You can also extend the capabilities of bendewey's UserDataContext class by adding properties that manipulate properties of the UserInfo and UserExtendedInfo classes. You might, for example, combine first name and last name.

    You may wish to implement INotifyPropertyChanged so that your controls update when you reset UserInfo and UserExtendedInfo.

    It may be architecturally preferable to entirely isolate the underlying UserInfo and UserExtendedInfo classes from the XAML by exposing the required properties directly in UserDataContext, thereby eliminating the need for Binding Path.

提交回复
热议问题