2

I have a class

public class DrawingCanvas : FrameworkElement

which uses MouseDragElementBehavior to implement dragging.

I want to drag the DrawingCanva like a layer in Photoshop relative to another layer

Working alone with (XAML on pic #1)

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

do allows dragging, but when I place DrawingCanvas inside ItemsControl and bind ItemsSource to collection of items, draggins doesn't work. (XAML on pic #2)

<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>

I don't understand why.

First case is the following XAML structure:

First case

And the second case is the following XAML structure:

Second case

Update:

I've found out that in second case with ItemsControl a DrawingCanvas.Parent = null

Dmitry Dyachkov
  • 1,715
  • 2
  • 19
  • 46

1 Answers1

0

In connection with that item.Parent inside ItemsControls is always null

I've found the solution in the following topics with explanation:

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

Update:

Now the Parent is not null, still the problem remains.

Update 2

The last lacking thing I had to define "extenders:DragHelper.CanDrag" inside ContentPresenter instead of

<ItemsControl.ItemTemplate>

The resulting template is as follows, so the issue is solved:

<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>
Community
  • 1
  • 1
Dmitry Dyachkov
  • 1,715
  • 2
  • 19
  • 46