【问题标题】:VS2010 cannot find type ControlTemplate even though System.Windows is referenced即使引用了 System.Windows,VS2010 也找不到类型 ControlTemplate
【发布时间】:2010-06-17 08:45:47
【问题描述】:

我正在尝试在这里学习 Silverlight,创建一个自定义控件模板,但是 VS2010 拒绝识别标记代码中的 ControlTemplate 类型,即使我已经引用了 System.WindowsSystem.Windows.Controls 程序集(由项目基于标准 Silverlight 应用程序模板时的默认设置)。我正在尝试重新创建this seen on another SO stack

我已尝试将此代码直接放入文件(即 ImageButton.xaml)中,仅此而已:

<ControlTemplate x:Key="ImageButtonTemplate">
    <Image Source="{TemplateBinding Content}" />
</ControlTemplate>

【问题讨论】:

  • 添加引用 System.Windows.Controls
  • 您是否引用了正确版本的程序集?
  • 你能把你的代码放在这里吗..,指出哪里出了问题会很有帮助..:)

标签: silverlight xaml reference controltemplate custom-controls


【解决方案1】:

在不了解更多上下文(例如您将其放置在什么类型的文件中,以及来自 Visual Studio 的确切错误是什么)的情况下,权威地回答这个问题有点困难。我想你会收到一个错误,例如:

The type 'ControlTemplate' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

或者可能:

Property 'Content' does not support values of type 'ControlTemplate'

这些是由于将模板放置在错误的位置造成的 - 例如,如果您创建一个新的UserControl(通过添加 -> 新项目)并删除文件的内容并粘贴到您的代码中,那么您将收到此错误,因为 xaml 没有对 ControlTemplate 的引用。

放置ControlTemplate 的最佳位置是可重用的地方,例如新的“资源字典”(同样,通过添加 -> 新项目 -> Silverlight 资源字典添加它),然后将代码放入其中 &lt;ResourceDictionary ...&gt;&lt;/ResourceDictionary&gt; 标签。

如果您想将它放在UserControl(第二个错误的来源)中,则应将其添加到该控件的Resources 部分,例如:

<UserControl.Resources>
    <ControlTemplate x:Key="ImageButtonTemplate">
        <Image Source="{TemplateBinding Content}" />
    </ControlTemplate>
</UserControl.Resources>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 2014-10-26
    • 2011-12-06
    相关资源
    最近更新 更多