WPF: How to bind to only one item in a collection, not using ItemsControl since I don't want to display all of them

前端 未结 3 1062
-上瘾入骨i
-上瘾入骨i 2020-12-09 17:14

I have this requirement, that I have a collection of items (ObservableCollection), but I only want to display the first item. The requirement comes from the fact that in mos

3条回答
  •  独厮守ぢ
    2020-12-09 17:34

    To bind to just one item from a collection, you can use the following syntax:

    {Binding Items[0]}
    

    Or to bind to a property of a single item from the collection:

    {Binding Items[0].Property}
    

    You can find out more about property path syntax from the Binding.Path Property page at MSDN... from the linked page:

    • Indexers of a property can be specified within square brackets following the property name where the indexer is applied. For instance, the clause Path=ShoppingCart[0] sets the binding to the index that corresponds to how your property's internal indexing handles the literal string "0". Multiple indexers are also supported.

提交回复
热议问题