Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to style a BulletDecorator in WPF?

Tags:

styles

wpf

Obviously it can have a style applied to it - what am trying to find out if its possible to define the Bullet element within the style, so you don't have to keep defining it over and over in the XAML

<BulletDecorator>
       <BulletDecorator.Bullet>
            ...my bullet UIElement here...
        </BulletDecorator.Bullet>
        <TextBlock>
            ... my text here...
        </TextBlock>
</BulletDecorator> 
like image 537
Jack Ukleja Avatar asked Nov 06 '09 00:11

Jack Ukleja


People also ask

What is a style in WPF?

Styles provide us the flexibility to set some properties of an object and reuse these specific settings across multiple objects for a consistent look. In styles, you can set only the existing properties of an object such as Height, Width, Font size, etc. Only default behavior of a control can be specified.


2 Answers

BulletDecorator.Bullet cannot be styled, and BulletDecorator is not a Control so it can't be templated.

However you can get the effect in pure XAML by defining a ControlTemplate for ContentControl like this:

<ControlTemplate x:Key="BulletTemplate" TargetType="{x:Type ContentControl}">
  <BulletDecorator>
    <BulletDecorator.Bullet>
      ...my bullet UIElement here...
    </BulletDecorator.Bullet>
    <ContentPresenter />
  </BulletDecorator>
</ControlTemplate>

Now you can use it like this:

<ContentControl Template="{StaticResource BulletTemplate}">
  <TextBlock />
</ContentControl>

If you only use it a few times, the "<ContentControl Template=..." technique works fine. If you are going to use it more often, you can define a MyBullet class:

public class MyBullet : ContentControl
{
  static MyBullet()
  {
    DefaultStyleKeyProperty.OverrideMetadata(typeof(MyBullet), new FrameworkPropertyMetadata(typeof(MyBullet));
  }
}

then move your ControlTemplate into Theme/Generic.xaml (or a dictionary merged into it) and wrap it with this:

<Style TargetType="{x:Type local:MyBullet}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate
        ...
    </Setter.Value>
  </Setter>
</Style>

If you do this, you can use:

<local:MyBullet>
  <TextBox />
</local:MyBullet>

anywhere in your application.

like image 140
Ray Burns Avatar answered Oct 13 '22 19:10

Ray Burns


Bullet is not a dependency property, so it can't be styled.

But you could of course declare your own classes that derive from Decorator and set the Bullet in the constructor, so you could write:

<local:MyDecorator>
  <TextBlock />
</local:MyDecorator>
like image 20
itowlson Avatar answered Oct 13 '22 20:10

itowlson