将ItemsControl绑定到ObservableCollection:额外生成的项目 [英] Binding Itemscontrol to ObservableCollection: extra generated item
问题描述
将 ItemsControl
绑定到 ObservableCollection< T>
会放置一个额外的 {NewItemPlaceholder}
在运行时控件中。如何删除?
Binding ItemsControl
to an ObservableCollection<T>
places an extra {NewItemPlaceholder}
in the control at runtime. How can I remove it?
NB 我见过与此问题相关的帖子,但这些帖子仅限于 DataGrid
,您可以在其中设置 CanUserAddRows
或 IsReadOnly
属性即可删除该项目。 ItemsControl
没有任何此类属性。
N.B. I have seen posts related to this problem but those are limited to DataGrid
where you can set CanUserAddRows
or IsReadOnly
properties to get rid of this item. ItemsControl
doesn't have any such property.
XAML
这是我的 ItemsControl
( MyPoints
是 ObservableCollection< T>
在底层ViewModel中):
Here's my ItemsControl
(MyPoints
is ObservableCollection<T>
in the underlying ViewModel):
<ItemsControl ItemsSource="{Binding MyPoints}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="FrameworkElement">
<Setter Property="Canvas.Left" Value="{Binding Path=X}" />
<Setter Property="Canvas.Top" Value="{Binding Path=Y}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type vm:PointVM}">
<Ellipse Width="10" Height="10" Fill="#88FF2222" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
这会在0,0处显示一个额外的点。 Live Property Explorer显示该点已绑定到 {NewItemPlaceholder}
对象。
This displays an extra point at 0,0. Live Property Explorer shows that this point is bound to the {NewItemPlaceholder}
object.
推荐答案
MSDN编写以下内容:
MSDN writes the following:
当
CollectionView
实现了IEditableCollectionView
将
NewItemPlaceholderPosition
设置为AtBeginning
或AtEnd
,将
NewItemPlaceholder
添加到集合中。NewItemPlaceholder
始终显示在集合中;它不参与分组,
排序或过滤。
When a
CollectionView
that implementsIEditableCollectionView
hasNewItemPlaceholderPosition
set toAtBeginning
orAtEnd
, theNewItemPlaceholder
is added to the collection. TheNewItemPlaceholder
always appears in the collection; it does not participate in grouping, sorting, or filtering.
链接: MSDN
希望将 NewItemPlaceholderPosition
设置为其他值,占位符将从集合中消失。
Hopefully if you set the NewItemPlaceholderPosition
to something else, the placeholder will disappear from the collection.
编辑:
如果您的 ObservableCollection< T>
被绑定到同样在其他地方(例如,对于 DataGrid
,您必须将 CanUserAddRows
设置为false),您必须处理其他项目。它将向您的收藏添加一个新的 NewItemPlaceholder
。
If your ObservableCollection<T>
is being binded to somewhere else as well (e.g.: to a DataGrid
, you have to set the CanUserAddRows
to false), you have to deal with that other item. It would add a new NewItemPlaceholder
to your collection.
这篇关于将ItemsControl绑定到ObservableCollection:额外生成的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!