下面的XAML代碼工作:問題與WPF衍生CONTROLTEMPLATES
<Window x:Class="DerivedTemplateBug.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DerivedTemplateBug"
Title="Window1" Height="300" Width="300">
<Button>
<Button.Template>
<ControlTemplate>
<Border BorderBrush="Black" BorderThickness="2">
<TextBlock>Testing!</TextBlock>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</Window>
現在,如果你定義了以下數據模板:
using System.Windows.Controls;
namespace DerivedTemplateBug
{
public class DerivedTemplate : ControlTemplate
{
}
}
然後交換的ControlTemplate派生類:
<Window x:Class="DerivedTemplateBug.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DerivedTemplateBug"
Title="Window1" Height="300" Width="300">
<Button>
<Button.Template>
<local:DerivedTemplate>
<Border BorderBrush="Black" BorderThickness="2">
<TextBlock>Testing!</TextBlock>
</Border>
</local:DerivedTemplate>
</Button.Template>
</Button>
</Window>
您收到以下錯誤信息:
找不到類型爲'DerivedTemplateBug.DerivedTemplate'的屬性'Content'的ContentPropertyAttribute無效。
誰能告訴我爲什麼這樣?
嗯,我沒有看到您所看到的錯誤? Derived模板中是否還有其他可能隱藏基礎ControlTemplate.Content屬性的內容?出於好奇,也是這個.NET 3.5或4.0。 – 2010-03-11 19:05:07
我使用.net 3.5和Visual Studio Professional 2008。這裏發佈的代碼是從我創建的乾淨解決方案逐字複製的。 – Farnk 2010-03-11 20:56:54