无法访问拖动装饰器模板

发布于 2024-10-30 16:18:39 字数 2568 浏览 0 评论 0原文

我使用了 Bea Stollnitz 提供的示例代码 (http://bea.stollnitz.com/ blog/?p=53),以便在我的应用程序中启用拖放以及拖动装饰器等。

一切正常,我的拖动装饰器显示良好,我拥有我想要的所有行为。

但是(是的,总有一个但是),我无法访问拖动装饰器的 DataTemplate,以便根据拖动的数据显示不同的数据。

我已经简化了代码,但基础知识仍然存在。

这是我的 DragAdorner 的 DataTemplate

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

这是创建 Adorner 的代码:

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

这是初始化装饰器的代码

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);
    }

DraggedData 将是 MyType 的列表,我将第一个项目作为我的 DragAdorner 的 ContentPresenter 的内容,因此可以应用 DataTemplate。

问题是,我想访问DataTemplate的numberContainer和number控件,以便在装饰器中显示拖动对象的数量。但无论我尝试什么,我都无法访问它,它以“此操作仅对应用了此模板的元素有效”结束。信息。

我曾经想过我可以做这样的事情:

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

因为 DataTemplate 应该适用于 ContentPresenter,但是不...... 有关信息,adornedElement 是发生拖动的 ListViewItem。

如果你有任何想法...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

情深如许 2024-11-06 16:18:39

好的,所以我已经找到了如何实现我想要的。

我不知道为什么我没有早点想到这一点,为什么我之前没有发现任何关于这一点的信息。

在尝试访问模板之前,我刚刚添加了一行:

this.UpdateLayout()

看起来它强制更新并“重新渲染”ContentPresenter 和 DataTemplate 对象,因此 ContentPresenter 实际上是由我的 DataTemplate 模板化的。

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.

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