WPF:设置绑定属性列表框结合 [英] WPF: Set Binding-property for ListBox-binding

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

问题描述

我有ItemsSource时,我绑定到存储在设定DataContext对象集合的列表框。这使得使用ToString()函数中显示的列表。

I have a listbox where I bind the ItemsSource to a collection stored in the set DataContext object. This makes the list displayed using the ToString() function.

<ListBox ItemsSource="{Binding SomeCollection}"></ListBox>

现在我想在集合中显示的对象属性。所以我想定义模板等要做到这一点就在绑定列表中的所有对象。我尝试了各种不同的方法没有成功的。我想要做这样的事情:

Now I want to display a property for the objects in the collection instead. So I want to define a template etc to do this on all the objects in the bound list. I tried a variety of different approaches without success. I'd like to do something like this:

<ListBox ItemsSource="{Binding SomeCollection}">
    <ListBox.Template>
        <ControlTemplate>                                
            <ListViewItem Content="{Binding ThePropertyOnElm}"></ListViewItem>
        </ControlTemplate>
    </ListBox.Template>
</ListBox>

谁能帮我做这个吗?

Can anyone help me make this right?

推荐答案

您不必指定模板,你可以使用的DisplayMemberPath属性,就像这样:

you don't need to specify a template, you can just use the DisplayMemberPath property, like so:

<ListBox ItemsSource="{Binding SomeCollection}" DisplayMemberPath="ThePropertyOnElm" />

希望这有助于!

hope this helps!

这篇关于WPF:设置绑定属性列表框结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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