2

我尝试使用 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>

即在模板中添加一个单选按钮

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

没有事件传递给 WPF 装饰层

建议添加此代码

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的错误

4

1 回答 1

1

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);
}
于 2012-11-21T22:13:09.993 回答