【发布时间】:2009-09-22 14:58:26
【问题描述】:
好的,我查看了其他问题,但似乎没有得到我的答案,所以希望这里有人可以。
非常简单的问题,为什么 DisplayMemberPath 属性没有绑定到项目?
<ComboBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding PromptList}" DisplayMemberPath="{Binding Name}" SelectedItem="{Binding Prompt}"/>
跟踪输出显示它正在尝试绑定到持有 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>
但在我看来,这样一个简单的任务太过分了。我可以使用相对源绑定吗?
【问题讨论】: