WPF 嵌套对象数据绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14141952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:52:35  来源:igfitidea点击:

WPF nested objects databinding

wpfdata-binding

提问by crk1337

Hi I am having issues with understanding WPF databinding with nested objects.

嗨,我在理解嵌套对象的 WPF 数据绑定时遇到问题。

I have a workgroup class containing a List of User_activation objects called ListMembersand I would like to display its properties. How do I access its nested properties? This class contains another object called Userthat has its username and ultimately I would like to display the username in the combobox instead of WPF_test.User_activation.

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

Below is the XAML code and corresponding layout:

下面是 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> 

Layout: http://i50.tinypic.com/ydy5h.png

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

Thank you!

谢谢!

回答by TimothyP

You need to set the ItemTemplate for the ComboBox

您需要为 ComboBox 设置 ItemTemplate

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

As an alternative, if you don't need anything complex you can bind the DisplayMemberPath

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

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

You use the "." to access properties like you would in normal c# code

您使用“。” 像在普通 c# 代码中一样访问属性

回答by Travis Banger

This is just a follow-up to the previous answer. I just discovered that in Bindings, you may use a leading period in a filesystem fashion:

这只是对上一个答案的跟进。我刚刚发现在 Bindings 中,您可以以文件系统方式使用前导句点:

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

That syntax adds nothing semantically, but in some cases makes the statement more readable (and XAML can certaintlyuse that!)

该语法在语义上没有添加任何内容,但在某些情况下使语句更具可读性(XAML肯定可以使用它!)

Here's a better example:

这是一个更好的例子:

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

where Thicknessis a property of Caliber.

Thickness的属性在哪里Caliber