2009-11-06 95 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> 
相關問題