WPF Combobox DisplayMemberPath [英] WPF Combobox DisplayMemberPath

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

问题描述

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



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

 < ComboBox Grid.Row =1Grid.Column =2ItemsSource ={Binding PromptList}DisplayMemberPath ={绑定名称}SelectedItem ={绑定提示}/> 

跟踪输出显示它试图绑定到持有IEnumerable而不是实际项目的类IEnumerable。我困惑为一个简单的方法来填充组合框,而不添加一行在xaml。



它只是为itemssource中的对象调用ToString()。我有一个工作是这样的:

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

但在我看来,这太简单了。我可以使用relativesource绑定吗?

解决方案

DisplayMemberPath 每个项目的显示字符串属性。在您的情况下,您可以将它设置为Name,而不是{Binding Name} p>

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

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}"/>

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.

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 specifies the path to the display string property for each item. In your case, you'd set it to "Name", not "{Binding Name}".

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

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