BulletDecorator.Bullet不能病急亂投醫,而BulletDecorator不是控制所以它不能被模板化。
不過你可以通過定義一個控件模板的ContentControl中像這樣獲得純XAML的效果:
<ControlTemplate x:Key="BulletTemplate" TargetType="{x:Type ContentControl}">
<BulletDecorator>
<BulletDecorator.Bullet>
...my bullet UIElement here...
</BulletDecorator.Bullet>
<ContentPresenter />
</BulletDecorator>
</ControlTemplate>
現在你可以使用這樣的:
<ContentControl Template="{StaticResource BulletTemplate}">
<TextBlock />
</ContentControl>
如果你只使用它幾次,「< ContentControl Template = ...」技術工作正常。如果你要更頻繁地使用它,你可以定義一個類MyBullet:
public class MyBullet : ContentControl
{
static MyBullet()
{
DefaultStyleKey.OverrideMetadata(typeof(MyBullet), new FrameworkPropertyMetadata(typeof(MyBullet));
}
}
然後移動控件模板爲主題/ Generic.xaml(或字典合併到它)以及與此包起來:
<Style TargetType="{x:Type local:MyBullet}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate
...
</Setter.Value>
</Setter>
</Style>
如果你這樣做,你可以使用:
<local:MyBullet>
<TextBox />
</local:MyBullet>
隨時隨地在你的應用。