很抱歉,标题有点模糊,但我无法想出更好的标题。
为了论证,我们假设我正在开发一个简单的绘图应用程序,用户只需点击并拖动绘制一条线(我不是在开发它,而只是为了保持简单)。
我有一个自定义形状的画线。现在我想根据需要在视图中添加新行,但是我想通过视图模型上的数据绑定使用ObservableCollection
属性来执行此操作。我可能会使用ItemsControl
。但当然ItemsControl
会自动定位它的项目,这不是我想要的。
有谁知道该怎么做?有没有办法禁用ItemsControl
的布局功能?
答案 0 :(得分:3)
您可以更改ItemsPanelTemplate
的{{1}},以便使用ItemsControl
代替Canvas
来保存其项目,然后使用StackPanel
将ItemContainerStyle
和Canvas.Top
属性绑定到数据对象以定位它们。
Canvas.Left
我有一篇关于ItemsControl的博客文章,它更详细地解释了<ItemsControl ItemsSource="{Binding MyCollection}">
<!-- ItemsPanelTemplate -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!-- ItemContainerStyle -->
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Top" Value="{Binding Y}" />
<Setter Property="Canvas.Left" Value="{Binding X}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
如果你感兴趣的话会如何运作。