【发布时间】:2016-09-09 03:02:41
【问题描述】:
将ItemsControl 绑定到ObservableCollection<T> 会在运行时在控件中放置一个额外的{NewItemPlaceholder}。我怎样才能删除它?
注意 我见过posts related to this problem,但仅限于DataGrid,您可以在其中设置CanUserAddRows 或IsReadOnly 属性以摆脱此项目。 ItemsControl 没有任何此类属性。
XAML
这是我的ItemsControl(MyPoints 在底层 ViewModel 中是 ObservableCollection<T>):
<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} 对象。
【问题讨论】:
-
实际上,以这种方式从 ViewModel 添加点对我来说效果很好:
MyPoints = new ObservableCollection<MyPoint> { new MyPoint { X = 2, Y = 3}, new MyPoint { X = 40, Y = 60} };您的 ObservableCollection 是否也绑定到其他地方?
标签: wpf data-binding itemscontrol