WPF绑定多个控件不同datacontexts

人气:1,281 发布:2022-09-10 标签: .net wpf data-binding resources

问题描述

我有一种情况,我真的不知道如何将数据绑定到托管的用户控件到多个datacontexts控制。

我要绑定的数据来自2班

 的UserInfo,UserExtendedInfo
 

用户控件的DataContext设置为的UserInfo这样我就可以绑定大多数控件容易做以下

 <标签名称=LblEmail文本={结合电子邮件}/>
 

不过,我不知道如何从UserExtendedInfo类很容易绑定属性。我最初的想法是要设置想要的使用从UserExtendedInfo的数据,所以我可以做同样的每个控件的DataContext的。但是,这似乎很麻烦,因为我将不得不手动indivdually分配各一台。对于UserExtendedInfo数据必须从数据库每次用户控件变得可见,使其没有得到同步的进账。

XAML:

 <标签名称=LblTest文本={结合语言环境}/>
 

code背后:

 私人小组UserManager_IsVisibleChanged(BYVAL发件人为System.Object的,BYVAL E上System.Windows.DependencyPropertyChangedEventArgs)
        如果DirectCast(e.NewValue,布尔)然后
            昏暗的用户作为的UserInfo = DirectCast(DataContext的,的UserInfo)

            如果用户状态并没有任何然后
                昏暗usrExt作为UserExtenedInfo = UserController.GetUserExtended(user.userID)

                LblTest.DataContext = usrExt
            其他
                抛出新的ArgumentException(用户ID不存在或小于1)
            结束如果
        结束如果
    结束小组
 

解决方案

我想,也许想想包装用户对象在一个单独的类,然后设置子面板包含数据的DataContext的属性。

例如:

 公共类UserDataContext
{
  公众的UserInfo的UserInfo {获得;组; }
  公共UserExtendedInfo UserExtendedInfo {获得;组; }
}
 

然后在 UserControl.xaml

 <! - 绑定的用户控件,应在其父设置,但为了清楚起见 - >
<用户控件的DataContext ={结合UserDataContext}>
  < StackPanel的>
    <电网的DataContext ={结合的UserInfo}>
       < TextBlock的文本={结合电子邮件}/>
    < /网格>
    <电网的DataContext ={结合UserExtendedInfo}>
       < TextBlock的文本={结合语言环境}/>
       < TextBlock的文本={结合AboutMe}/>
    < /网格>
  < / StackPanel的>
< /用户控件>
 

这假定您的UserInfo类有电子邮件的一个属性。

这是你的UserExtendedInfo类有现场和AboutMe的属性

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 classes

UserInfo, UserExtendedInfo

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}" />

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}" />

Code Behind:

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

解决方案

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.

For example:

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

Then in your 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>

This assumes that your UserInfo class has a property of Email

and

That your UserExtendedInfo class has a property of Locale and AboutMe

380