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

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

问题描述

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

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.

我能想到的第一个解决方案(如果其他人更好,请推荐其他人)是绑定到这个集合(但不使用 ItemsControl)并定义一个 DataTemplateSelector派生类(返回 DataTemplate 以显示唯一的一项,或者返回具有 ... 和弹出窗口的 DateTemplate详细信息,基于集合中的项目数)并将其用作 ContentTemplateSelector.

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.

但现在我的问题是:我的 DataTemplate 在 XAML 中的外观如何,以便它们只能显示集合中的第一项?显然我不能有 ItemsControl.

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.

更新:

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

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).

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

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

并且在 MyTemplateSelector 中我不确定如何使用它,因为没有对我的集合的引用,因为它被定义为资源并且它没有 MyCollection<的信息/代码>.然而,事实证明它非常简单,DataTemplate 可以在不知道名称或任何其他引用的情况下引用索引项.就像这样:

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}

您可以从 Binding.Path 属性 页面...来自链接页面:

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

• 可以在应用索引器的属性名称后面的方括号内指定属性的索引器.例如,子句 Path=ShoppingCart[0] 将绑定设置为与您的财产的内部索引处理文字字符串0"的方式相对应的索引.还支持多个索引器.

• 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天全站免登陆