WPF 组合框 DisplayMemberPath [英] WPF Combobox DisplayMemberPath

查看:37
本文介绍了WPF 组合框 DisplayMemberPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我看了其他问题,似乎没有得到我的答案,所以希望这里有人可以.

Ok, I looked at other questions and didn't seem to get my answer so hopefully someone here can.

很简单的问题,为什么 DisplayMemberPath 属性没有绑定到项目?

Very simple question why does the DisplayMemberPath property not bind to the item?

<ComboBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding PromptList}" DisplayMemberPath="{Binding Name}" SelectedItem="{Binding Prompt}"/>

跟踪输出显示它正在尝试绑定到持有 IEnumerable 的类,而不是 IEnumerable 中的实际项目.我对填充组合框的简单方法感到困惑,而无需在 xaml 中添加一行行.

The trace output shows that it is trying to bind to the class holding the IEnumerable not the actual item in the IEnumerable. I'm confused as to a simple way to fill a combobox without adding a bunch a lines in xaml.

它只是为 itemssource 中的对象调用 ToString().我有一个解决方法:

It simply calls the ToString() for the object in itemssource. I have a work around which is this:

<ComboBox Grid.Row="1" Grid.Column="2" ItemsSource="{Binding PromptList}"  SelectedItem="{Binding Prompt}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

但在我看来,这对于这样一个简单的任务来说太过分了.我可以使用相对源绑定吗?

But in my opinion it's too much for such a simple task. Can I use a relativesource binding?

推荐答案

DisplayMemberPath 指定每个项目的显示字符串属性的路径.在您的情况下,您将其设置为 "Name",而不是 "{Binding Name}".

DisplayMemberPath specifies the path to the display string property for each item. In your case, you'd set it to "Name", not "{Binding Name}".

这篇关于WPF 组合框 DisplayMemberPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆