【问题标题】:Cannot access adorners on element that has no adorners无法访问没有装饰器的元素上的装饰器
【发布时间】:2012-01-29 03:50:59
【问题描述】:

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

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

但是当我尝试使用一些事件生成组件(例如数据模板中的按钮或单选按钮)对其进行自定义时,我在拖放时遇到此错误

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

在这条线上

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>

即在模板中添加单选按钮

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

No events passed to WPF adorner layer

建议添加此代码

VisualCollection 视觉儿童; 框架元素@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 上的错误

【问题讨论】:

  • 所以似乎没有人受到我的问题的启发。我找到了 bea stollnitz 解决方案的另一种实现。使用codeproject.com/KB/WPF/gong-wpf-dragdrop.aspx,我终于用事件生成模板进行了拖放。这个解决方案看起来更像是解决方案背后的代码,而不是真正的 mvvm 解决方案(因为我们直接在视图模型中通过拖放事件进行管理),但它可以在没有错误的情况下工作,并且还有另一个优势,可以订购放置的项目。
  • 您找到解决方案了吗?我认为相同的代码有同样的问题。

标签: wpf mvvm drag-and-drop adorner


【解决方案1】:

我想在这里发帖说我找到了解决方案。看完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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-02
    • 2011-09-09
    • 2011-06-25
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    相关资源
    最近更新 更多