WPF嵌套对象数据绑定

问题描述:

您好我有理解WPF数据绑定与嵌套对象的问题。WPF嵌套对象数据绑定

我有一个包含User_activation对象列表的工作组类,名为ListMembers我想显示它的属性。我如何访问它的嵌套属性?这个类包含另一个对象用户有它的用户名,最终我想在组合框中显示用户名而不是WPF_test.User_activation。

下面是XAML代码和相应的布局:

<ListView x:Name="ListViewWorkgroups" VerticalAlignment="Top" Height="Auto" Width="Auto" ItemsSource="{Binding listWorkgroups}"> 
       <ListView.View> 
        <GridView> 
         <GridViewColumn Width="auto" Header="Workgroup" DisplayMemberBinding="{Binding Name}"></GridViewColumn> 
         <GridViewColumn Width="auto" Header="Skills"> 
          <GridViewColumn.CellTemplate> 
           <DataTemplate> 
            <ComboBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ListSkills}" ></ComboBox> 
           </DataTemplate> 
          </GridViewColumn.CellTemplate> 
         </GridViewColumn> 
         <GridViewColumn Width="auto" Header="Members"> 
          <GridViewColumn.CellTemplate> 
           <DataTemplate > 
            <ComboBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ListMembers}" ></ComboBox> 
           </DataTemplate> 
          </GridViewColumn.CellTemplate> 
         </GridViewColumn> 
        </GridView> 
       </ListView.View> 
      </ListView> 

布局:http://i50.tinypic.com/ydy5h.png

谢谢!

+0

那么问题是什么? – Doorknob

您需要设置ItemTemplate中的组合框

<ComboBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ListMembers}" > 
<ComboBox.ItemTemplate> 
    <DataTemplate> 
     <TextBlock Text="{Binding User.Username}"/> 
    </DataTemplate> 
</ComboBox.ItemTemplate> 
</ComboBox> 

作为替代方案,如果你不需要任何复杂的可以绑定的DisplayMemberPath

<ComboBox IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ListMembers}" DisplayMemberPath="{Binding User.Username}"/> 

您可以使用“”访问属性就像你会在正常的C#代码

+0

谢谢蒂莫西。我得到了它的工作。 – crk1337

这只是前一个答案的后续行动。我才发现,原来在绑定,您可以在文件系统的方式使用前导期:

<ComboBox ItemsSource="{Binding .ListMembers}"> 
<DataTemplate> 
    <TextBlock Text="{Binding .User.Username}"/> 
</DataTemplate> 

这句法语义上都不会给,但在某些情况下,使表述更加可读(和XAML可以certaintly使用! )

这里有一个更好的例子:

<ComboBox ItemsSource="{Binding Caliber}" DisplayMemberPath=".Thickness" /> 

其中ThicknessCaliber属性。