2009-11-06 92 views
8

很明显,它可以应用到它的风格 - 什么我试图找出如果可能的样式中定义的子弹元素,让你不必守在XAML遍地定义它是否可以在WPF中设计BulletDecorator?

<BulletDecorator> 
     <BulletDecorator.Bullet> 
      ...my bullet UIElement here... 
     </BulletDecorator.Bullet> 
     <TextBlock> 
      ... my text here... 
     </TextBlock> 
</BulletDecorator> 

回答

11

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> 

随时随地在你的应用。

1

项目符号不是依赖属性,所以它不能被设置样式。

但是,当然,你可以声明自己的类,从装饰派生,并设置在构造函数中的子弹,所以你可以写:

<local:MyDecorator> 
    <TextBlock /> 
</local:MyDecorator> 
相关问题