1

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!

0

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.