【问题标题】:Dragging behavior is not working for items in ItemsControl: MouseDragElementBehavior拖动行为不适用于 ItemsControl 中的项目:MouseDragElementBehavior
【发布时间】:2014-07-17 15:32:24
【问题描述】:

我有课

public class DrawingCanvas : FrameworkElement

它使用 MouseDragElementBehavior 来实现拖动。

我想将 DrawingCanva 像 Photoshop 中的一个图层一样相对于另一个图层拖动

单独使用(图片 #1 上的 XAML)

<Grid x:Name="_layoutRootControl">
    <Canvas
        Grid.Column="0">
        <canvas:DrawingCanvas />
        <canvas:DrawingCanvas />
    </Canvas>
</Grid>

do 允许拖动,但是当我将 DrawingCanvas 放在 ItemsControl 内时 并将 ItemsSource 绑定到项目集合,拖动不起作用。 (图片 #2 上的 XAML)

<ItemsControl ItemsSource="{Binding Source={StaticResource Locator}, Path=LayersViewModel.Layers}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                <Setter Property="Canvas.Left" Value="0" />
                <Setter Property="Canvas.Top" Value="0" />
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <canvas:DrawingCanvas />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

我不明白为什么。

第一种情况是以下 XAML 结构:

第二种情况是下面的XAML结构:

更新:

我发现在 ItemsControl 的第二种情况下,DrawingCanvas.Parent = null

【问题讨论】:

  • 你能提供更多细节吗?你想拖动什么,DrawingCanvas 中的项目,还是DrawingCanvas 本身?您能否也提供您的ItemsControl 的 XAML?
  • 是的,我会更新主题并提供更多细节
  • 我发现在第二种情况下 ItemsControl DrawingCanvas.Parent = null

标签: wpf drag itemscontrol


【解决方案1】:

与该项目相关。ItemsControls 中的父级始终为空

我在以下主题中找到了解决方案并附有说明:

Why does the Parent property of a container of an ItemsControl return null and not the Panel it sits on?

Combining ItemsControl with draggable items - Element.parent always null

更新:

现在 Parent 不为空,问题仍然存在。

更新 2

最后缺少的东西 我必须在 ContentPresenter 而不是

中定义“extenders:DragHelper.CanDrag”
<ItemsControl.ItemTemplate>

生成的模板如下,问题就解决了:

<ItemsControl
    ItemsSource="{Binding Source={StaticResource Locator}, Path=LayersViewModel.Layers}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas IsItemsHost="True"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property=**"extenders:DragHelper.CanDrag"** Value="True"/>
            <Setter Property="Canvas.Left" Value="{Binding Path=X}" />
            <Setter Property="Canvas.Top" Value="{Binding Path=Y}" />
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <surfaces:DrawingCanvas />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

【讨论】:

  • 请问那个 DragHelper 是从哪里来的?我在 .NET 4.5 Framework 和 Blend 的 Behavior SDK 中都没有找到它。
  • DragHelper 来自我的代码。在这里找到它,在stackoverflow上
  • @Dmitry 你能提供 DragHelper 的代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2017-03-30
  • 1970-01-01
  • 2011-04-23
  • 1970-01-01
相关资源
最近更新 更多