WPF:如何仅绑定到集合中的一项,不使用 ItemsControl,因为我不想显示所有项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18659667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF: How to bind to only one item in a collection, not using ItemsControl since I don't want to display all of them
提问by tete
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.
我有这个要求,我有一个项目集合(ObservableCollection),但我只想显示第一个项目。要求来自这样一个事实,即在大多数情况下,集合只包含一个项目。并且由于空间限制,即使集合中有多个项目,我们也希望显示项目的数量、第一个的详细信息(与之前的情况相同)和一个...
符号以指示用户有更多的项目。当鼠标悬停在 UI 元素上时,弹出窗口最终将显示所有项目。
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
.
我能想到的第一个解决方案(如果他们更好,请建议其他人)是绑定到这个集合(但不使用ItemsControl
)并定义一个DataTemplateSelector
派生类(它返回DataTemplate
以显示唯一的一个项目,或者DateTemplate
具有...
以及更多详细信息的弹出窗口,基于集合中的项目数)并将其用作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
.
但现在我的问题是:我的两个DataTemplate
在 XAML 中的外观如何,以便它们只能显示集合中的第一项?显然我不能有一个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
按照一个答案的建议使用。但我想,因为我需要绑定到整个集合(而不是单个索引项),并使用 a根据集合中的项数DataTemplateSelector
选择适当DataTemplate
的项。代码如下所示:
<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:
而在MyTemplateSelector
我不知道如何使用它,因为那里是因为它被定义为资源,它不具有的信息,我的收藏没有提及MyCollection
。然而,事实证明它非常简单,DataTemplate
可以在不知道名称或任何其他引用的情况下引用索引项。就像这样:
<DataTemplate>
<TextBlock Text="{Binding [0].PropertyName}" />
<DataTemplate />
回答by Sheridan
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 Propertypage at MSDN... from the linked page:
您可以从MSDN的Binding.Path 属性页面找到有关属性路径语法的更多信息...从链接页面:
? 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.
? 可以在应用索引器的属性名称后面的方括号内指定属性的索引器。例如,该子句
Path=ShoppingCart[0]
将绑定设置为与您的属性的内部索引处理文字字符串“0”的方式相对应的索引。还支持多个索引器。
回答by Nitesh
Try this
尝试这个
<ContentControl Content="{Binding YourCollection[0]}">
<ContentControl.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"></TextBlock>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
回答by superjos
Ok, late to the party but I thought I'd share my 2 cents anyway: I'd better go with a dumber (XAML-)view and a view-model closer to your presentation needs.
好吧,聚会迟到了,但我想无论如何我都会分享我的 2 美分:我最好使用更简单的 (XAML-) 视图和更接近您的演示需求的视图模型。
Translated: instead of mapping your existing view-model (or raw data) and its collection of items directly to the view, I suggest to map that to an appropriate view-model showing something like a YourItemViewModel FirstItem
property and a bool HasMore
property. That second view-model would be easily unit-testable to make sure it behaves propertly, and would be easily mapped to a view with less logic, so to avoid possible hard-to-test problems in view.
翻译:我建议不要将现有的视图模型(或原始数据)及其项目集合直接映射到视图,而是将其映射到显示YourItemViewModel FirstItem
属性和bool HasMore
属性之类的适当视图模型。第二个视图模型很容易进行单元测试,以确保它的行为正常,并且可以轻松映射到逻辑较少的视图,从而避免视图中可能出现的难以测试的问题。