【问题标题】:How do I databind to children of a child object?如何将数据绑定到子对象的子对象?
【发布时间】:2010-01-04 21:26:03
【问题描述】:

下面是我的 XML 对象示例。由于隐私,我不得不取出实际数据,但你应该了解事情的要点。目前我的绑定工作得很好,但我无法得到一个盒子来填写必要的孙子信息。列表框工作正常,这可能是我没有看到的逻辑问题。

每个医生可以有一个或多个医生办公室,但每个医生办公室只属于一个组,该组本身就是一个集合。 Nettiers 启用了一个 IdSource 属性,该属性允许您定义孙元素而不是遍历层次结构。这就是我要填写的内容。 GroupIdSource 正在填充,但绑定根本没有抓住它。

<TextBlock Grid.Row="0" Grid.Column="1" Background="blue" FontSize="12" 
           FontWeight="Bold" >
  **<Label Content="{Binding Path=GroupIdSource.GroupName}**"/>




<Doctors xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <EntityState> </EntityState>
      <Id> </Id>
      <DisplayName> </DisplayName>
      <FirstName> </FirstName>
      <LastName> </LastName>
      <MiddleName> </MiddleName>
      <Credentials xsi:nil="true" />
      <AddDate> </AddDate>
      <AddUserId> </AddUserId>
      <ChangeDate> </ChangeDate>
      <ChangeUserId> </ChangeUserId>
      <ProviderTypeId> </ProviderTypeId>
      <SpecialtyId> </SpecialtyId>
      <DoctorOfficesCollection>
        <DoctorOffices>
          <EntityState> </EntityState>
          <DoctorsId> </DoctorsId>
          <ForeignId> </ForeignId>
          <OriginalForeignId> </OriginalForeignId>
          <Active> </Active>
          <Address1> </Address1>
          <Address2> </Address2>
          <Address3> </Address3>
          <City> </City>
          <State> </State>
          <Zip> </Zip>
          <PrimaryPhone> </PrimaryPhone>
          <Fax> </Fax>
          <AlternatePhone1 xsi:nil="true" />
          <AlternatePhone2> </AlternatePhone2>
          <OfficeName xsi:nil="true" />
          <OfficeDisplayName> </OfficeDisplayName>
          <AddDate> </AddDate>
          <AddUserId> </AddUserId>
          <ChangeDate> </ChangeDate>
          <ChangeUserId> </ChangeUserId>
          <CommonOfficeIdentifier> </CommonOfficeIdentifier>
          <GroupId> </GroupId>
          <OfficeNotesCollection />
          <HasChanges> </HasChanges>
          <GroupIdSource>
            <EntityState> </EntityState>
            <Id> </Id>
            <GroupCode> </GroupCode>
            <GroupName> </GroupName>
            <DoctorOfficesCollection />
          </GroupIdSource>
        </DoctorOffices>
      </DoctorOfficesCollection>
      <DoctorTaxonomyCollection>
        <DoctorTaxonomy>
          <EntityState> </EntityState>
          <DoctorId> </DoctorId>
          <OriginalDoctorId> </OriginalDoctorId>
          <TaxonomySpecializationId> </TaxonomySpecializationId>
          <OriginalTaxonomySpecializationId> </OriginalTaxonomySpecializationId>
          <TaxonomyTypeId> </TaxonomyTypeId>
          <OriginalTaxonomyTypeId> </OriginalTaxonomyTypeId>
          <TaxonomyClassificationId> </TaxonomyClassificationId>
          <OriginalTaxonomyClassificationId> </OriginalTaxonomyClassificationId>
          <AddDate> </AddDate>
          <AddUserId> </AddUserId>
          <ChangeDate xsi:nil="true" />
          <ChangeUserId xsi:nil="true" />
        </DoctorTaxonomy>
      </DoctorTaxonomyCollection>
      <DoctorHome>
        <EntityState> </EntityState>
        <DoctorsId> </DoctorsId>
        <OriginalDoctorsId> </OriginalDoctorsId>
        <Address1> </Address1>
        <Address2 xsi:nil="true" />
        <Address3 xsi:nil="true" />
        <City> </City>
        <State> </State>
        <Zip> </Zip>
        <PrimaryPhone xsi:nil="true" />
        <Fax xsi:nil="true" />
        <AddDate> </AddDate>
        <AddUserId> </AddUserId>
        <ChangeDate> </ChangeDate>
        <ChangeUserId> </ChangeUserId>
      </DoctorHome>
      <DoctorLicenses>
        <EntityState> </EntityState>
        <DoctorsId> </DoctorsId>
        <OriginalDoctorsId> </OriginalDoctorsId>
        <LicenseNumber> </LicenseNumber>
        <DeaNumber xsi:nil="true" />
        <NpiNumber> </NpiNumber>
        <Bc_StateLicenseNumber> </Bc_StateLicenseNumber>
        <ChampusIdNumber> </ChampusIdNumber>
        <MedicaidLicenseNumber> </MedicaidLicenseNumber>
        <Medicaid1500LicenseNumber> </Medicaid1500LicenseNumber>
        <MedicareLicenseNumber> </MedicareLicenseNumber>
        <MedchiNumber xsi:nil="true" />
        <UpinNumber> </UpinNumber>
        <AddDate> </AddDate>
        <AddUserId> </AddUserId>
        <ChangeDate> </ChangeDate>
        <ChangeUserId> </ChangeUserId>
      </DoctorLicenses>
      <TaxonomyTypeIdTaxonomyClassificationIdTaxonomySpecializationIdTaxonomySpecializationCollection_From_DoctorTaxonomy />
      <SpecialtyIdSource>
        <EntityState> </EntityState>
        <Id> </Id>
        <OriginalId> </OriginalId>
        <Description> </Description>
        <Flag> </Flag>
        <DoctorsCollection />
      </SpecialtyIdSource>
      <ProviderTypeIdSource>
        <EntityState> </EntityState>
        <Id> </Id>
        <OriginalId> </OriginalId>
        <Description> </Description>
        <Flag> </Flag>
        <DoctorsCollection />
      </ProviderTypeIdSource>
      <EditingUser> </EditingUser>
    </Doctors>

这是我的完整 XAML 也可以提供帮助。我有一个 。数据绑定在上面工作,但它与我的数据视图的绑定方式有所不同。它直接绑定到对象,然后是它的子对象...您可以在 NPINumber 的第一个列表框中看到它...

<Grid x:Class="Views.DoctorMasterDetailView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:ad="clr-namespace:WavelengthIS.Infrastructure.AttachedProperties;assembly=WavelengthIS.Infrastructure"
      xmlns:clr="clr-namespace:System;assembly=mscorlib"
      xmlns:c="clr-namespace:WavelengthIS.Infrastructure.Converters;assembly=WavelengthIS.Infrastructure"
      xmlns:vw="clr-namespace:WavelengthIS.Referee.Modules.DoctorMasterDetail.ViewModel"
      MinHeight="480" MinWidth="640">
    <Grid.Resources>
        <c:StringTrimmingConverter x:Key="trimmingConverter" />
        <c:CombineStringsConverter x:Key="CombineStringsConverter"/>
        <c:TextToVisibilityConverter x:Key="TextToVisibilityConverter"/>
    </Grid.Resources>
    <Grid.DataContext>
        <vw:DoctorMasterDetailViewModel/>
    </Grid.DataContext>
    <Grid.ColumnDefinitions>
        <ColumnDefinition MinWidth="100" Width="150" />
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <GridSplitter Grid.Column="0" 
              HorizontalAlignment="Right" 
              VerticalAlignment="Stretch"
              Background="Black" 
              ShowsPreview="false"
              Width="2"/>
    <ListBox x:Name="DoctorHeaderList" Grid.Column="0" Background="Green" Margin="0,0,2,0"
             ItemsSource="{Binding DoctorsList}"
             SelectedItem="{Binding SelectedDoctor}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Border BorderBrush="Black" 
                        BorderThickness="1"
                        Background="White"
                        Width="{Binding ElementName=DoctorHeaderList, Path=ActualWidth}"
                        CornerRadius="5"
                        Margin="5"
                        Padding="5">
                        <TextBlock Text="{Binding Path=DisplayName, Converter={StaticResource trimmingConverter}}" Foreground="Black" FontSize="14"/>
                    </Border>
                    <TextBlock Text="{Binding Path=DoctorLicenses.NpiNumber, StringFormat='NPI Number: {0}'}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <ListBox x:Name="DoctorOfficesList" Grid.Column="1" Background="Orange"
             ItemsSource="{Binding Path=SelectedDoctor.DoctorOfficesCollection}" HorizontalContentAlignment="Stretch">
    <!--DESIGN TIME BINDING ONLY!!!!!!!!!!!!!!!!!!!!-->
    <!--<ListBox x:Name="DoctorOfficesList" Grid.Column="1" Background="Orange"
             ItemsSource="{Binding Path=Offices}" HorizontalContentAlignment="Stretch">-->
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderThickness="2" BorderBrush="Black" CornerRadius="5" Margin="1" >
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition MinHeight="10" />
                            <RowDefinition MinHeight="35" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Row="0" Grid.Column="0" Background="red" >
                            <Label Content="Physician Number: "/>
                            <Label Content="{Binding Path=ForeignId, StringFormat='Physician Number: {0}'}"
                                    FontSize="12" FontWeight="Bold" />
                        </TextBlock>
                        <TextBlock Grid.Row="0" Grid.Column="1" Background="blue" FontSize="12" FontWeight="Bold" >
                            <Label Content="{Binding Path=GroupIdSource.GroupName}"/>
                        </TextBlock>
                        <StackPanel  Orientation="Horizontal" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Background="green">
                            <StackPanel Orientation="Vertical" >
                                <TextBlock Margin="5,0,0,0" Text="{Binding Path=Address1, Converter={StaticResource trimmingConverter}}"/>
                                <TextBlock Margin="5,0,0,0" Text="{Binding Path=Address2, Converter={StaticResource trimmingConverter}}"/>
                                <TextBlock Margin="5,0,0,0" Text="{Binding Path=Address3, Converter={StaticResource trimmingConverter}}"/>
                                <TextBlock Margin="5,0,0,0">
                            <TextBlock.Text>
                                <MultiBinding  StringFormat="{}{0}, {1}  {2}">
                                    <Binding Path="City"/>
                                    <Binding Path="State"/>
                                    <Binding Path="Zip"/>
                                </MultiBinding>
                            </TextBlock.Text>
                                </TextBlock>
                            </StackPanel>
                            <StackPanel Orientation="Vertical" Margin="5,0,0,0" FlowDirection="RightToLeft">
                                <TextBlock Text="{Binding Path=PrimaryPhone, StringFormat='Primary Phone: {0}'}"/>
                                <TextBlock Text="{Binding Path=Fax, StringFormat='Fax: {0}'}" />
                                <TextBlock Text="{Binding Path=AlternatePhone1, StringFormat='Alternate Phone 1: {0}'}" 
                                       Visibility="{Binding Path=AlternatePhone1, Converter={StaticResource TextToVisibilityConverter}}" />
                                <TextBlock Text="{Binding Path=AlternatePhone2, StringFormat='Alternate Phone 2: {0}'}"
                                        Visibility="{Binding Path=AlternatePhone2, Converter={StaticResource TextToVisibilityConverter}}" />
                            </StackPanel>
                        </StackPanel>
                    </Grid>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>


</Grid>

【问题讨论】:

    标签: wpf data-binding binding


    【解决方案1】:

    当绑定 XML 数据源时,您应该使用 XPath 而不是 Path 来获得所需的元素/属性。

    编辑:您需要以某种方式将您的对象设置为控件(或其祖先之一)的数据上下文,然后您可以引用它的任何属性或嵌套属性。例如在你的情况下:

    ...{Binding Path=DoctorOfficesCollection[0].GroupIdSource.GroupName}...
    

    【讨论】:

    • 实际元素不是 xml。我将对象的结构转换为 xml 只是为了帮助理解对象的布局。
    • 好吧,我将 Listbox 的 itemsource 设置为 {binding path=selecteddoctor.officecollection} 是否有某种方式我必须操纵各个 listitems 才能知道要获取集合对象的哪个实例?我以为我的 NPI 示例类似,但我记得该表实际上是 1:1 关系而不是 1:many...
    【解决方案2】:

    经过大量阅读,我终于明白了这一点。但实际上,除非我遗漏了关于 wpf 控件如何工作的内容,否则我的代码在语义上没有任何变化。我可能会慢慢解除这些更改,因为无论如何它都在测试视图中,看看我是否可以复制问题。以下是可能遇到类似问题并需要知道如何执行此操作的任何人的工作代码。

    <GroupBox 
            x:Name="DoctorOffices" 
            Header="Doctor Offices" 
            DataContext="{Binding Path=SelectedDoctor.DoctorOfficesCollection}"
            Grid.Column="1" 
            Grid.Row="0">
            <ListBox x:Name="DoctorOfficesListBox"  Background="Green"
                 ItemsSource="{Binding}"
                     IsSynchronizedWithCurrentItem="true">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border BorderThickness="2" BorderBrush="Black" CornerRadius="5" Margin="1" >
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition MinHeight="10" />
                                    <RowDefinition MinHeight="35" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Row="0" Grid.Column="0" Background="red" >
                                <Label Content="Physician Number: "/>
                                <Label Content="{Binding Path=ForeignId, StringFormat='Physician Number: {0}'}"
                                        FontSize="12" FontWeight="Bold" />
                                </TextBlock>
                                <TextBlock Background="white" Grid.Row="0" Grid.Column="1"
                                           Text="{Binding Path=GroupIdSource.GroupName}" />
                                <StackPanel  Orientation="Horizontal" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Background="green">
                                    <StackPanel Orientation="Vertical" >
                                        <TextBlock Margin="5,0,0,0" Text="{Binding Path=Address1, Converter={StaticResource trimmingConverter}}"/>
                                        <TextBlock Margin="5,0,0,0" Text="{Binding Path=Address2, Converter={StaticResource trimmingConverter}}"/>
                                        <TextBlock Margin="5,0,0,0" Text="{Binding Path=Address3, Converter={StaticResource trimmingConverter}}"/>
                                        <TextBlock Margin="5,0,0,0">
                                <TextBlock.Text>
                                    <MultiBinding  StringFormat="{}{0}, {1}  {2}">
                                        <Binding Path="City"/>
                                        <Binding Path="State"/>
                                        <Binding Path="Zip"/>
                                    </MultiBinding>
                                </TextBlock.Text>
                                        </TextBlock>
                                    </StackPanel>
                                    <StackPanel Orientation="Vertical" Margin="5,0,0,0" FlowDirection="RightToLeft">
                                        <TextBlock Text="{Binding Path=PrimaryPhone, StringFormat='Primary Phone: {0}'}"/>
                                        <TextBlock Text="{Binding Path=Fax, StringFormat='Fax: {0}'}" />
                                        <TextBlock Text="{Binding Path=AlternatePhone1, StringFormat='Alternate Phone 1: {0}'}" 
                                           Visibility="{Binding Path=AlternatePhone1, Converter={StaticResource TextToVisibilityConverter}}" />
                                        <TextBlock Text="{Binding Path=AlternatePhone2, StringFormat='Alternate Phone 2: {0}'}"
                                            Visibility="{Binding Path=AlternatePhone2, Converter={StaticResource TextToVisibilityConverter}}" />
                                    </StackPanel>
                                </StackPanel>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </GroupBox>
    

    正如你所看到的,我所做的唯一不同的事情是添加一个组框集到列表框的原始数据绑定,然后将 itemsource 更改为 {bindings}。其他任何更改都不应产生任何实际影响。

    【讨论】:

    • 也许 issynchronizedwithcurrentitem 是票。我不认为在我的另一个观点上我有这个水平。但我会验证的。
    猜你喜欢
    • 2015-09-29
    • 2013-02-03
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    相关资源
    最近更新 更多