WPF binding multiple controls to different datacontexts

后端 未结 4 2001
不知归路
不知归路 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:17

    Here is the simplest method of all, and it works very well.

    In the code-behind where you set the context, simply use an anonymous type containing all the desired values:

    DataContext = new
    {
      info = FetchUserInfoFromDatabase(),
      extendedInfo = FetchExtendedUserInfoFromDatabase(),
    };
    

    In the XAML you can bind to anything:

    
      
        
        
          
          ...
    

提交回复
热议问题