【问题标题】:WPF phone 8 The attachable property Resources was not found in type DataTemplateWPF电话8在类型DataTemplate中找不到可附加属性资源
【发布时间】:2015-02-06 05:59:31
【问题描述】:

我试图在运行时为 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 中找不到可附加属性 Resources

我是否遗漏了包含中的某些内容?

即使此示例没有使用它,转换器也会在 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

【问题讨论】:

    标签: c# wpf windows-phone-8


    【解决方案1】:

    我认为您将 WPF 行为与 Windows 和 Windows Phone 8 的 SilverLight XAML 分发混淆了。不幸的是,ResourcesFrameworkElement 基类的属性,而DataTemplate 派生自较低级别的DependencyObject班级。因此,它不支持资源集合。

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

    【讨论】:

    • 感谢您的澄清,您建议的修复是我最初的问题 - 我的顶级是 ,当我将转换器添加到它的资源时,加载 xaml stackoverflow.com/questions/28350731/…
    • 我有点担心您选择模板的方法。 It seems you're actually attempting to parse XAML when it would be easier to either select a static instance of a DataTemplate resource or UserControl resource and assign it programmatically, and this may be the source of your woes in making use of the converter.问题实际上可能是架构问题。
    • 我也很担心 ;) 如果您有一个强类型对象,我可以看到如何根据数据模板的目标类型进行选择。我所拥有的是字符串的“行”(在设计时不知道有多少列需要再次评估为布尔值,直到运行时才知道。)我正在尝试动态创建一个网格元素这些值占据列表框的每一行。
    • ...这部分工作正常——我需要的最后一步是将转换器应用于绑定(用于将字符串数据评估为布尔数据的真或假)——这就是我当转换器的静态资源驻留在该 xaml 字符串中时,似乎无法解决
    • 感谢您对原始问题的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    相关资源
    最近更新 更多