无法访问没有装饰器的元素上的装饰器

发布于 2024-12-23 02:25:34 字数 2865 浏览 2 评论 0原文

我尝试使用 MVVM 在 WPF 上进行一些拖放操作,

我从 Bea Stollnitz 找到了此链接 http://bea.stollnitz提出了一个解决方案 在这里使用 DragDropHelper : https://github.com/bstollnitz /old-wpf-blog/tree/master/46-DragDropListBox

但是当我尝试使用一些事件生成组件(例如按钮或数据模板中的单选按钮,我在拖动时遇到此错误drop

“无法访问没有装饰器的元素上的装饰器。”

在这一行

this.adornerLayer.Update(this.AdornedElement);

来轻松重现它

您可以通过下载 bea.stollnitz.com/files/46/DragDropListBox.zip并替换

    <DataTemplate x:Key="pictureTemplate">
        <DataTemplate.Resources>
            <Style TargetType="Image">
                <Setter Property="Width" Value="50" />
                <Setter Property="Height" Value="50" />
                <Setter Property="Margin" Value="10" />
            </Style>
        </DataTemplate.Resources>
            <Image Source="{Binding Path=Location}" />
    </DataTemplate>

    <DataTemplate x:Key="pictureTemplate">
        <DataTemplate.Resources>
            <Style TargetType="Image">
                <Setter Property="Width" Value="50" />
                <Setter Property="Height" Value="50" />
                <Setter Property="Margin" Value="10" />
            </Style>
        </DataTemplate.Resources>
        <Grid>
            <Image Source="{Binding Path=Location}" />
            <RadioButton />
        </Grid>
    </DataTemplate>

ie 在模板中添加单选按钮

,我已经找到了一些链接,但没有一个链接解释了解决问题的明确方法。

没有事件传递到 WPF 装饰层

建议添加此代码

VisualCollection视觉儿童; FrameworkElement @object;

public CustomAdorner(UIElement adornedElement) :
    base(adornedElement)
{
    visualChildren = new VisualCollection(this);
    @object = new Button {Content = "prova"};
    visualChildren.Add(@object);
}
protected override Visual GetVisualChild(int index)
{
    return visualChildren[index];
}

但我确定在哪里添加它以及此链接的相同内容

http://social.msdn.microsoft.com/Forums/en/wpf/thread/e6643abc-4457-44aa-a3ee-dd389c88bd86?prof=required

提出

private bool IsItemDisconnected(object item)
{
  bool isDisconnected = false;

  var itemType = item.GetType();
  if (itemType.FullName.Equals("MS.Internal.NamedObject"))
  {
    isDisconnected = true;
  }

  return isDisconnected;
}

最后一个链接讨论 .NET 4 问题,但我在 3.5 上也有这个错误

I try to make some drag&drop on WPF using MVVM

I found this link from Bea Stollnitz http://bea.stollnitz which propose a solution
with a DragDropHelper here : https://github.com/bstollnitz/old-wpf-blog/tree/master/46-DragDropListBox

but when i try to customize it with some events generating components such as button or radioButton in datatemplate, i have this error on drag & drop

"Cannot access adorners on element that has no adorners."

on this line

this.adornerLayer.Update(this.AdornedElement);

you can reproduce it easily by downloading bea.stollnitz.com/files/46/DragDropListBox.zip

and replacing

    <DataTemplate x:Key="pictureTemplate">
        <DataTemplate.Resources>
            <Style TargetType="Image">
                <Setter Property="Width" Value="50" />
                <Setter Property="Height" Value="50" />
                <Setter Property="Margin" Value="10" />
            </Style>
        </DataTemplate.Resources>
            <Image Source="{Binding Path=Location}" />
    </DataTemplate>

by

    <DataTemplate x:Key="pictureTemplate">
        <DataTemplate.Resources>
            <Style TargetType="Image">
                <Setter Property="Width" Value="50" />
                <Setter Property="Height" Value="50" />
                <Setter Property="Margin" Value="10" />
            </Style>
        </DataTemplate.Resources>
        <Grid>
            <Image Source="{Binding Path=Location}" />
            <RadioButton />
        </Grid>
    </DataTemplate>

i.e. adding a radiobutton in the template

i already find some links but none of them explain a clear way to solve the issue.

No events passed to WPF adorner layer

that propose to add this code

VisualCollection visualChildren;
FrameworkElement @object;

public CustomAdorner(UIElement adornedElement) :
    base(adornedElement)
{
    visualChildren = new VisualCollection(this);
    @object = new Button {Content = "prova"};
    visualChildren.Add(@object);
}
protected override Visual GetVisualChild(int index)
{
    return visualChildren[index];
}

but i'm sure where to add it and same thing for this link

http://social.msdn.microsoft.com/Forums/en/wpf/thread/e6643abc-4457-44aa-a3ee-dd389c88bd86?prof=required

that propose

private bool IsItemDisconnected(object item)
{
  bool isDisconnected = false;

  var itemType = item.GetType();
  if (itemType.FullName.Equals("MS.Internal.NamedObject"))
  {
    isDisconnected = true;
  }

  return isDisconnected;
}

this last link talk about a .NET 4 issue but i also have the bug on 3.5

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

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

发布评论

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

评论(1

執念 2024-12-30 02:25:34

我想在这里发帖说我已经找到了解决方案。阅读拖放错误:无法访问后没有装饰器的元素上的装饰器

if (this.adornerLayer != null && this.contentPresenter.Content != null)
{
    this.adornerLayer.Update(this.AdornedElement);
}

I wanted to post on here that I had found a solution to this. After reading Drag drop error : Cannot access adorners on element that has no adorners

if (this.adornerLayer != null && this.contentPresenter.Content != null)
{
    this.adornerLayer.Update(this.AdornedElement);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文