1

您好我在理解 WPF 数据绑定与嵌套对象时遇到问题。

我有一个工作组类,其中包含一个名为ListMembers的 User_activation 对象列表,我想显示它的属性。如何访问其嵌套属性?此类包含另一个名为User的对象,该对象具有其用户名,最终我想在组合框中显示用户名,而不是 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

谢谢!

4

2 回答 2

2

您需要为 ComboBox 设置 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# 代码中一样访问属性

于 2013-01-03T15:35:16.617 回答
0

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:

<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 certaintly use that!)

Here's a better example:

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

where Thickness is a property of Caliber.

于 2013-11-30T01:44:56.453 回答