1
votes

I've used the sample code provided by Bea Stollnitz (http://bea.stollnitz.com/blog/?p=53), in order to enable drag and drop in my application, and drag adorner, etc.

Everything works fine, my drag adorner is well displayed, I have all the behavior I want.

But (yes there is always a but), I cannot access the DataTemplate of the Drag Adorner, in order to display different data depending on the dragged data.

I have simplified the code, but the basics are still there.

This is the DataTemplate of my DragAdorner

<DataTemplate x:Key="DragAndDropTemplate" DataType="{x:Type MyType}">
        <Grid>
            <Grid Opacity="0.5">
                <Border x:Name="HeaderBorder" CornerRadius="2" BorderThickness="1" Margin="5,2,5,2">
                   <Border x:Name="InsideBorder" CornerRadius="2" BorderThickness="1">
                      <TextBlock x:Name="number" Text="{Binding Name}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White"/>
                   </Border>
                </Border>
            </Grid>
            <Border Width="17" Height="17" BorderBrush="White" HorizontalAlignment="Center" VerticalAlignment="Center" CornerRadius="1" x:Name="numberContainer" Visibility="Collapsed">
                <TextBlock x:Name="number" Text="80" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White"/>
            </Border>
        </Grid>
    </DataTemplate>

This is the code that create the Adorner :

if (this.draggedAdorner == null)
        {
            var adornerLayer = AdornerLayer.GetAdornerLayer(source);
            this.draggedAdorner = new DraggedAdorner(draggedData, dataTemplate, source, adornerLayer);
        }

And this is the code that init an adorner

public DraggedAdorner(List dragDropData, DataTemplate dragDropTemplate, FrameworkElement adornedElement, AdornerLayer adornerLayer)
        : base(adornedElement)
    {
        this.adornerLayer = adornerLayer;
        this.contentPresenter = new ContentPresenter();

        this.contentPresenter.Content = dragDropData[0];

        this.contentPresenter.ContentTemplate = dragDropTemplate;
        this.adornerLayer.Add(this);
    }

The draggedData, will be a list of MyType, I get the first item as the content of the ContentPresenter of my DraggedAdorner, so the DataTemplate can apply.

The problem is, I want to access the numberContainer and number control of the DataTemplate, in order to display the number of dragged object, in the adorner. But I cannot manage to access it, whatever I try, It ends with the "This operation is valid only on elements that have this template applied." message.

I have tought I could do something like this :

this.contentPresenter.ContentTemplate.FindName("number", this.contentPresenter);

Since the DataTemplate should apply to the ContentPresenter, but nope... For information the adornedElement is the ListViewItem from which the drag occurs.

If you have any idea...

1

1 Answers

1
votes

Ok, so I have found how to achieve what I wanted.

I don't know why it didn't comes to mind earlier, and why I didn't found anything about this before.

I have just added a single line before trying to access the template :

this.UpdateLayout()

Looks like it forces the ContentPresenter and DataTemplate object to be update and "re-rederend" so the ContentPresenter is really templated by my DataTemplate.