2010-06-17 46 views
2

我想在這裏學習的Silverlight,創建自定義的控件模板,但是VS2010拒絕承認ControlTemplate類型的標記代碼,即使我已經提到的System.WindowsSystem.Windows.Controls程序集(在基於標準Silverlight應用程序模板的基礎上默認情況下)。我正在嘗試重新創建this seen on another SO stackVS2010找不到輸入控件模板即使System.Windows被引用

我試過直接把這個代碼放到一個文件(即ImageButton.xaml),並沒有別的:

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

添加refernce System.Windows.Controls – Malcolm 2010-06-17 10:29:37

+0

是否也有;) – 2010-06-17 11:00:27

+0

您是否引用了正確版本的程序集? – 2010-06-17 11:02:50

回答

0

這是一個有點難以回答這個問題權威性不知道多一點的情況下,如什麼類型的文件將您放入此文件中,以及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(通過Add - > New Item)並刪除文件的內容並粘貼代碼,然後你會得到這個錯誤,因爲xaml沒有引用ControlTemplate

把你ControlTemplate最好的地方是什麼地方可重複使用的,如一個新的「資源字典」(再次,通過添加它添加 - >新建項目 - > Silverlight的資源字典),然後把你的代碼<ResourceDictionary ...></ResourceDictionary>標籤。

如果你想將它放在一個UserControl(在第二誤差源)範圍內,那麼你應該將它添加到該控件的Resources部分,例如:

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