0

我有一个装饰器,用于在拖放时提供视觉反馈。Adorner 在内部使用 ContentPresenter 来显示(非可视)内容。当我使用 Visual 作为内容时,(可视)对象会正确呈现,但是当我使用非可视对象时,Adorner 及其 ContentPresenter 仅调用 ToString() 并且不应用给定的任何 DataTemplates。

有人知道我做错了什么,DataTemplates 不用于渲染 Adorner 层上的非可视对象吗?

这是一个简化的示例代码:

装饰者:

internal class ContentAdorner : System.Windows.Documents.Adorner
{
    private readonly ContentPresenter contentPresenter = new ContentPresenter();

    public ContentAdorner(UIElement adornedElement)
        : base(adornedElement)
    {
    }

    public object Content
    {
        get
        {
            return this.contentPresenter.Content;
        }

        set
        {
            this.contentPresenter.Content = value;
        }
    }

    protected override int VisualChildrenCount
    {
        get
        {
            return 1;
        }
    }

    protected override Visual GetVisualChild(int index)
    {
        return this.contentPresenter;
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        Rect rect = new Rect(finalSize);

        this.contentPresenter.Arrange(rect);

        return finalSize;
    }

    protected override Size MeasureOverride(Size constraint)
    {
        this.contentPresenter.Measure(constraint);

        return this.contentPresenter.DesiredSize;
    }

    protected override System.Collections.IEnumerator LogicalChildren
    {
        get
        {
            ArrayList list = new ArrayList()
            {
                this.contentPresenter,
            };

            return list.GetEnumerator();
        }
    }
}

在 Adorner 中用作 Content 的非可视类:

    internal class Model {}

主窗口.xaml

<Window x:Class="Adorner.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Adorner"
    Title="MainWindow" Height="350" Width="525" AllowDrop="True">
<Window.Resources>
    <DataTemplate DataType="{x:Type local:Model}">
        <Rectangle Fill="Yellow" Width="50" Height="50"/>
    </DataTemplate>
</Window.Resources>
<Grid Name="myGrid">
    <ContentControl MouseMove="ContentControl_MouseMove"
                    DragOver="ContentControl_DragOver">
        <ContentControl.Content>
            <local:Model/> 
        </ContentControl.Content>
    </ContentControl>
</Grid>

MainWindow.xaml 后面的代码:

    private void ContentControl_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            Model model = ((ContentControl)sender).Content as Model;

            DataObject dataObject = new DataObject("Model", model);

            DragDrop.DoDragDrop(sender as DependencyObject, dataObject, DragDropEffects.Move);
        }
    }

    private void ContentControl_DragOver(object sender, DragEventArgs e)
    {
        Model model = (Model)e.Data.GetData("Model");

        ContentAdorner adorner = new ContentAdorner(this.myGrid)
        {
            Content = model,
        };

        AdornerLayer layer = AdornerLayer.GetAdornerLayer(this.myGrid);

        layer.Add(adorner);
    }

似乎这个问题处理了同样的问题:点击这里

4

1 回答 1

0

我找到了一个解决方案:

Adorner 层不知道 MainWindow 的资源。您可以手动将特定的 DataTemplates 添加到 Adorner 的资源中,也可以将 Themes/Generic.xaml 与 AssemblyInfo.cs 文件中的 ThemeInfo 属性一起使用来指定 DataTemplates。

于 2013-09-14T12:44:38.683 回答