将选择框添加到 ItemsControl 中?
我有一个带有 Canvas ItemPanel 的 WPF ItemsControl。它从内部源绘制,显示许多自动生成的矩形。
截至目前,它看起来像这样:
<Window
// .. NameSpaces
>
<Window.Resources>
<DataTemplate x:Key="binTemplate">
<Rectangle x:Name="Rect" VerticalAlignment="Bottom"
Canvas.Left="10" StrokeThickness="0">
// .. Databinding Height, Width, etc
</Rectangle>
</DataTemplate>
</Window.Resources>
<DockPanel x:Name="LayoutRoot" LastChildFill="True">
<ItemsControl
ItemsSource="{Binding Bins}"
ItemTemplate="{StaticResource binTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas MouseDown="Canvas_MouseDown" MouseMove="Canvas_MouseMove" MouseUp="Canvas_MouseUp">
<Rectangle x:Name="Selection" Canvas.Left="0" Canvas.Top="0" Visibility="Hidden" />
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DockPanel>
</Window>
我现在也尝试实现一个选择框.. 好吧.. 选择我的矩形。 =) 我尝试将框添加到 ItemsControl PanelTemplate 中,并使用 Canvas 鼠标事件控制它,但似乎不允许我这样做。错误消息是“无法显式修改用作 ItemsControl 的 ItemsPanel 的 Panel 的子集合。ItemsControl 为 Panel 生成子元素。”
实现这种行为的首选方式是什么?我考虑过覆盖另一个画布(使用负边距)并在其中绘制选择框,但这似乎是一个糟糕的技巧。
提前致谢!
I have a WPF ItemsControl with a Canvas ItemPanel. Drawing from an internal source, it displays lots of automatically generated Rectangles.
As of now, it looks like this:
<Window
// .. NameSpaces
>
<Window.Resources>
<DataTemplate x:Key="binTemplate">
<Rectangle x:Name="Rect" VerticalAlignment="Bottom"
Canvas.Left="10" StrokeThickness="0">
// .. Databinding Height, Width, etc
</Rectangle>
</DataTemplate>
</Window.Resources>
<DockPanel x:Name="LayoutRoot" LastChildFill="True">
<ItemsControl
ItemsSource="{Binding Bins}"
ItemTemplate="{StaticResource binTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas MouseDown="Canvas_MouseDown" MouseMove="Canvas_MouseMove" MouseUp="Canvas_MouseUp">
<Rectangle x:Name="Selection" Canvas.Left="0" Canvas.Top="0" Visibility="Hidden" />
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DockPanel>
</Window>
I now try to implement a selection box, too.. well.. select my Rectangles. =)
I tried just adding the box into the ItemsControl PanelTemplate, and controlling it with the Canvas mouse events, but it appears I am not allowed to do so. The error message is "Cannot explicitly modify Children collection of Panel used as ItemsPanel for ItemsControl. ItemsControl generates child elements for Panel."
What is the preferred way to implement such behavior? I thought about overlaying another canvas (using negative Margin) and drawing the selection box in there, but that seems like a bad hack.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应将选择框添加到
Canvas
中,而应将其添加为Canvas
上的Adorner
。要检索Canvas
的AdornerLayer
,您可以使用AdornerLayer.GetAdornerLayer
方法,或将Canvas
包装在AdornerDecorator
并使用其AdornerLayer
属性。Rather than adding the selection box to the
Canvas
, you should add it as anAdorner
over theCanvas
. To retrieve theAdornerLayer
for theCanvas
, you can use theAdornerLayer.GetAdornerLayer
method, or wrap theCanvas
in anAdornerDecorator
and use itsAdornerLayer
property.