WPF 将多个控件绑定到不同的数据上下文 [英] WPF binding multiple controls to different datacontexts

查看:15
本文介绍了WPF 将多个控件绑定到不同的数据上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我真的不知道如何将数据绑定到托管在 UserControl 中的控件到多个数据上下文.

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

我要绑定的数据来自2个类

The data i want to bind comes from 2 classes

UserInfo, UserExtendedInfo

UserControl 的数据上下文设置为 UserInfo,因此我可以轻松地绑定大多数控件,执行以下操作

The datacontext of the UserControl is set to UserInfo so i can bind most controls easily doing the following

<Label Name="LblEmail" Text="{Binding Email}" />

但是我不知道如何轻松地从 UserExtendedInfo 类绑定属性.我最初的想法是设置想要使用 UserExtendedInfo 数据的每个控件的数据上下文,以便我可以这样做.但这似乎很麻烦,因为我必须单独手动分配每个.每次 UserControl 变得可见时,都必须从数据库中获取 UserExtendedInfo 的数据,以免它不同步.

However I don't know how to bind properties from the UserExtendedInfo class easily. My initial thought was to set the datacontext of each control that want's to use the data from UserExtendedInfo so i could do the same. But this seems cumbersome as i would have to manually assign each one indivdually. The data for UserExtendedInfo must be fetched from the database each time the UserControl becomes visible so that it doesn't get out of sync.

XAML:

<Label Name="LblTest" Text="{Binding Locale}" />

背后的代码:

Private Sub UserManager_IsVisibleChanged(ByVal sender As System.Object, ByVal e As System.Windows.DependencyPropertyChangedEventArgs)  
        If DirectCast(e.NewValue, Boolean) Then
            Dim user As UserInfo = DirectCast(DataContext, UserInfo)

            If user IsNot Nothing Then
                Dim usrExt As UserExtenedInfo = UserController.GetUserExtended(user.userID)

                LblTest.DataContext = usrExt
            Else
                Throw New ArgumentException("UserId doesn't exist or is less than 1")
            End If
        End If
    End Sub

推荐答案

我可能会考虑将您的用户对象包装在一个单独的类中,然后设置包含数据的子面板的 DataContext 属性.

I would maybe think about wrapping your user object in a seperate class then setting the DataContext properties of sub-panels that contain the data.

例如:

public class UserDataContext
{
  public UserInfo UserInfo { get; set; }
  public UserExtendedInfo UserExtendedInfo { get; set; }
}

然后在您的 UserControl.xaml 中:

<!-- Binding for the UserControl should be set in its parent, but for clarity -->
<UserControl DataContext="{Binding UserDataContext}">
  <StackPanel>
    <Grid DataContext="{Binding UserInfo}">
       <TextBlock Text="{Binding Email}" />
    </Grid>
    <Grid DataContext="{Binding UserExtendedInfo}">
       <TextBlock Text="{Binding Locale}" />
       <TextBlock Text="{Binding AboutMe}" />
    </Grid>
  </StackPanel>
</UserControl>

这里假设你的 UserInfo 类有一个 Email 属性

This assumes that your UserInfo class has a property of Email

您的 UserExtendedInfo 类具有 Locale 和 AboutMe 属性

That your UserExtendedInfo class has a property of Locale and AboutMe

这篇关于WPF 将多个控件绑定到不同的数据上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆