WPF嵌套对象数据绑定 [英] WPF nested objects databinding

查看:128
本文介绍了WPF嵌套对象数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



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

 < ListView x:Name =ListViewWorkgroupsVerticalAlignment =TopHeight =AutoWidth =AutoItemsSource ={Binding listWorkgroups}> 
< ListView.View>
< GridView>
< GridViewColumn Width =autoHeader =工作组DisplayMemberBinding ={Binding Name}>< / GridViewColumn>
< GridViewColumn Width =autoHeader =Skills>
< GridViewColumn.CellTemplate>
< DataTemplate>
< ComboBox IsSynchronizedWithCurrentItem =TrueItemsSource ={Binding ListSkills}>< / ComboBox>
< / DataTemplate>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< GridViewColumn Width =autoHeader =Members>
< GridViewColumn.CellTemplate>
< DataTemplate>
< ComboBox IsSynchronizedWithCurrentItem =TrueItemsSource ={Binding ListMembers}>< / ComboBox>
< / DataTemplate>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< / GridView>
< /ListView.View>
< / ListView>

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



谢谢!

解决方案

您需要为ComboBox设置ItemTemplate

  ; ComboBox IsSynchronizedWithCurrentItem =TrueItemsSource ={Binding ListMembers}> 
< ComboBox.ItemTemplate>
< DataTemplate>
< TextBlock Text ={Binding User.Username}/>
< / DataTemplate>
< /ComboBox.ItemTemplate>
< / ComboBox>

另外,如果你不需要任何复杂的东西,你可以绑定DisplayMemberPath

 < ComboBox IsSynchronizedWithCurrentItem =TrueItemsSource ={Binding ListMembers}DisplayMemberPath ={Binding User.Username}/> 

您使用。访问属性,就像在正常的c#代码


中一样

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

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

Below is the XAML code and corresponding layout:

<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

Thank you!

解决方案

You need to set the ItemTemplate for the ComboBox

<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

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

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

这篇关于WPF嵌套对象数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆