Adorner不在ContentPresenter中的Resources中应用DataTemplates

时间:2013-09-06 13:47:35

标签: c# wpf datatemplate adorner contentpresenter

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

是否有人知道我错误的是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中用作内容的非可视类:

    internal class Model {}

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

似乎这个问题处理同样的问题:Click here

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

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