更新时间:2023-01-17 08:08:56
您需要为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