简体   繁体   English

在后面的代码中创建DataTemplate时出错

[英]Error on creating DataTemplate in code behind

I'm trying to create a DataTemplate in code behind with the following: 我试图用下面的代码在代码中创建一个DataTemplate:

StringReader stringReader = new StringReader(
    @"<DataTemplate      
        xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
        xmlns:local=""clr-namespace:Clarity.Demo.ListDragDrop;assembly=Clarity.Demo.ListDragDrop"">
        <DataTemplate.Resources>
           <local:PlayerAdorner x:Key=""adorner""/>
        </DataTemplate.Resources>
        <local:PlayerControl 
            Margin=""5""
            local:DragDropHelper.AdornerLayer = ""adornLayer""
            local:DragDropHelper.DragDropControl = ""{StaticResource adorner}""
            local:DragDropHelper.DropTarget = ""lineup""
            local:DragDropHelper.IsDragSource = ""true"" />
        </DataTemplate > ");

XmlReader xmlReader = XmlReader.Create(stringReader);
DataTemplate laneTemplate = XamlReader.Load(xmlReader) as DataTemplate;

I get an XAML exception saying that 'x' is a space for a non declared name in line 5. I guess there's something wrong with x:Key=""adorner"" , but it's strange because if instead of creating this in code I copy it and paste on the XAML it works! 我收到一个XAML异常,说“ x”是第5行中未声明名称的空间。我猜x:Key=""adorner""有点问题,但这很奇怪,因为如果不是在代码中创建它,我复制它并粘贴到可以正常工作的XAML上!

I'm not sure this is the right way of doing it. 我不确定这是否是正确的方法。 The original code had the adorner resource in Window.Resources, but I didn't figured out how to access it when creating the DataTemplate in code, so I put it as a DataTemplate.Resource. 原始代码在Window.Resources中具有装饰资源,但是在代码中创建DataTemplate时我不知道如何访问它,因此我将其作为DataTemplate.Resource放置。

Does anyone have any clue on this? 有人对此有任何线索吗?

包括名称空间:

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM