WPF:设置绑定属性列表框结合绑定、属性、列表、WPF

2023-09-04 01:07:38 作者:我离开你会不会挽留

我有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!

 
精彩推荐