WPF phone 8 在 DataTemplate 类型中找不到可附加的 属性 资源

WPF phone 8 The attachable property Resources was not found in type DataTemplate

我试图在运行时为 ListBox 的 ContentTemplateSelected 踢出一个 DataTemplate。数据类型和值在运行时是未知的。这是一个例子。当我这样称呼时:

var dataTemplate = (DataTemplate)XamlReader.Load(xaml.ToString());

在生成的 xaml:

<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:DSI.MyProject.Converters;assembly=DSI.MyProject"
>
    <DataTemplate.Resources>
        <converters:StringValueToBoolConverter x:Key="stringValueToBoolConverter" />
    </DataTemplate.Resources>   
    <Grid>
      <Grid.ColumnDefinitions>
         <ColumnDefinition Width="128" />
      </Grid.ColumnDefinitions>
      <Border BorderThickness="1" BorderBrush="Black" Grid.Column="0" Background="{StaticResource PhoneContrastBackgroundBrush}" >
          <CheckBox IsChecked="{Binding [CheckEd], Mode=TwoWay}" Padding="2" HorizontalAlignment="Left" />
      </Border>
   </Grid>
</DataTemplate>

我收到错误:

在 DataTemplate 类型中找不到可附加的 属性 资源

我是否遗漏了一些内容?

尽管此示例未使用它,但转换器将用于 IsChecked 绑定。

似乎对这个人有用: Dynamically added DataTemplate - StaticResource for Converter can't be found

我原来的问题:WPF using converter in datatemplate created from xaml text and XamlReader on phone 8

我认为您将 WPF 行为与 Windows 和 Windows Phone 8 的 SilverLight XAML 分布混淆了。不幸的是,Resources is a property of the FrameworkElement base class, while DataTemplate derives from the lower level DependencyObject class.因此,它不支持资源收集。

作为解决方法,请考虑将转换器作为您的应用程序或页面资源的一部分,而不是直接作为 DataTemplate 的一部分。