WPF:如何绑定到一个集合中的一个项目,而不是使用ItemsControl,因为我不想显示它们 [英] WPF: How to bind to only one item in a collection, not using ItemsControl since I don't want to display all of them

查看:161
本文介绍了WPF:如何绑定到一个集合中的一个项目,而不是使用ItemsControl,因为我不想显示它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个要求,我有一个项目集(ObservableCollection),但我只想显示第一个项目。要求来自于在大多数情况下,集合仅包含一个项目的事实。并且由于空间限制,即使收藏中有多个项目,我们希望显示项目的数量,第一个的详细信息(与之前的情况相同的演示文稿)和 ... 符号表示给用户有更多的项目。当鼠标在UI元素上方时,弹出窗口将最终显示所有项目。



我可以想到的第一个解决方案(请建议其他人,如果他们更好)是绑定到此集合(但不使用 ItemsControl )并定义一个 DataTemplateSelector 派生类(返回 DataTemplate 显示唯一的一个项目,或 DateTemplate ,其中有 ... 和弹出式窗口中的更多细节,根据集合中的项目数量),并将其用作 ContentTemplateSelector



但是现在我的问题是:我的两个 DataTemplate 如何在XAML中看起来像这样,以便它们只能显示集合中的第一个项目?显然,我不能有一个 ItemsControl



更新:



现在我已经设法使它工作,并同意这个问题可以关闭(我已经有一些答案,我不能删除它)。



我实际上知道如何绑定集合中的一个特定项目,但这不是我困惑的地方。我觉得我应该使用 ContentControl 作为一个答案建议。但是我想,因为我需要绑定到整个集合(而不是单个索引项),并使用一个 DataTemplateSelector 选择正确的 DataTemplate 根据集合中的项目数量。代码如下所示:

 < ContentControl Content ={Binding MyCollection}
ContentTemplateSelector ={ staticResource MyTemplateSelector}/>

而在 MyTemplateSelector 我不知道如何使用它,因为没有引用我的集合,因为它被定义为资源,它没有 MyCollection 的信息。但是,原来是非常简单的, DataTemplate 可以引用索引的项目而不知道名称或任何其他引用。简单如下:

 < DataTemplate> 
< TextBlock Text ={Binding [0] .PropertyName}/>
< DataTemplate />


解决方案

要绑定到集合中的一个项目,可以使用以下语法:

  {Binding Items [0]} 
pre>

或绑定到集合中单个项目的属性:

  {Binding Items [0] .Property} 

您可以找到更多关于属性路径来自 Binding.Path属性的语法页面在MSDN ...从链接页面:


•属性的索引器可以在属性名称后面的方括号内指定,索引器被应用。例如,条款 Path = ShoppingCart [0] 设置绑定到与属性的内部索引处理文字字符串0对应的索引。还支持多个索引器。



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 most of the case, the collection only contains one item. And due to the space limit, even if there is more than one items in the collection, we'd like to display the number of the items, details of the first one (same presentation as prior situation) and a ... symbol to indicate to the user that there is more items. And when the mouse is over the UI element a popup will eventually display all items.

The first solution I can think of (please suggest others if they are better) is to bind to this collection (but not using an ItemsControl) and define a DataTemplateSelector derived class (which return either the DataTemplate to display the only one item, or the DateTemplate which has the ... and the popup for more details, based on the number of items in the collection) and use it as ContentTemplateSelector.

But now my question: how both of my DataTemplate would look like in XAML, so that they can display only the first item in the collection? Obviously I can't have a ItemsControl.

UPDATE:

Now I have managed to make it work and agree this question can be closed (I can't delete it anymore since there is already some answers).

I actually knew how to bind to one certain item in the collection, but this was not where I am confused. I felt I should use ContentControl as one answer suggests. But I thought since I need to bind to the whole collection (not to single indexed item), and use a DataTemplateSelector to select the proper DataTemplate based on the number of items in the collection. The code would look like this:

<ContentControl Content="{Binding MyCollection}"
          ContentTemplateSelector="{StaticResource MyTemplateSelector}" />

And in MyTemplateSelector I wasn't sure how to use it since there is no reference to my collection because it is defined as resource and it doesn't have the information of MyCollection. However, it turned out to be very simple, the DataTemplate can refer to an indexed item without knowing the name or any other reference. Simply like this:

<DataTemplate>
   <TextBlock Text="{Binding [0].PropertyName}" />
<DataTemplate />

解决方案

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.

这篇关于WPF:如何绑定到一个集合中的一个项目,而不是使用ItemsControl,因为我不想显示它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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