将选择框添加到 ItemsControl 中?

发布于 2024-09-03 08:21:16 字数 1386 浏览 1 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送你一个梦 2024-09-10 08:21:16

您不应将选择框添加到 Canvas 中,而应将其添加为 Canvas 上的Adorner。要检索 CanvasAdornerLayer,您可以使用 AdornerLayer.GetAdornerLayer 方法,或将 Canvas 包装在 AdornerDecorator 并使用其 AdornerLayer 属性。

Rather than adding the selection box to the Canvas, you should add it as an Adorner over the Canvas. To retrieve the AdornerLayer for the Canvas, you can use the AdornerLayer.GetAdornerLayer method, or wrap the Canvas in an AdornerDecorator and use its AdornerLayer property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文