好的,我查看了其他问题,似乎没有得到我的答案,所以希望有人在这里.
非常简单的问题为什么DisplayMemberPath属性不绑定到项目?
<ComboBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding PromptList}" DisplayMemberPath="{Binding Name}" SelectedItem="{Binding Prompt}"/>
Run Code Online (Sandbox Code Playgroud)
跟踪输出显示它正在尝试绑定到持有IEnumerable的类而不是IEnumerable中的实际项.我很困惑一个简单的方法来填充一个组合框而不在xaml中添加一串线.
它只是在itemssource中为对象调用ToString().我有一个解决方法是:
<ComboBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding PromptList}" SelectedItem="{Binding Prompt}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
但在我看来,对于这么简单的任务来说太过分了.我可以使用relativesource绑定吗?