crk1337
crk1337

Reputation: 13

WPF nested objects databinding

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!

Upvotes: 1

Views: 5441

Answers (2)

Travis Banger
Travis Banger

Reputation: 717

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.

Upvotes: 0

TimothyP
TimothyP

Reputation: 21765

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

Upvotes: 2

Related Questions