将ItemsControl绑定到ObservableCollection:额外生成的项目 [英] Binding Itemscontrol to ObservableCollection: extra generated item

查看:177
本文介绍了将ItemsControl绑定到ObservableCollection:额外生成的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 implements IEditableCollectionView has NewItemPlaceholderPosition set to AtBeginning or AtEnd, the NewItemPlaceholder is added to the collection. The NewItemPlaceholder 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屋!

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