Jack Ukleja
Jack Ukleja

Reputation: 13511

Is it possible to style a BulletDecorator in 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> 

Upvotes: 12

Views: 6964

Answers (3)

Kevin D.
Kevin D.

Reputation: 1

I was looking for a simpler solution to design the bulletdecorator for a radiobutton separately and wanted to present my solution here, since I have not found any good examples.

<ControlTemplate TargetType="{x:Type RadioButton}" x:Key="RadioButtonControlTemplate">
    <BulletDecorator Background="Transparent" Cursor="Hand">
        <BulletDecorator.Bullet>
            <Grid Height="15" Width="15">
                <Ellipse x:Name="OffStyle" Margin="2" Fill="{DynamicResource RadioButtonOff}" Visibility="Visible" />
                <Ellipse x:Name="OnStyle" Margin="2" Fill="{DynamicResource RadioButtonOn}" Visibility="Hidden" />
            </Grid>
        </BulletDecorator.Bullet>
    </BulletDecorator>
    <ControlTemplate.Triggers>
        <Trigger Property="IsChecked" Value="true">
            <Setter TargetName="OnStyle" Property="Visibility" Value="Visible"/>
            <Setter TargetName="OffStyle" Property="Visibility" Value="Hidden" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

You can use it like this

<Viewbox Grid.Row="1" Height="50" Width="50">
   <RadioButton GroupName="RadioButtonGroup" HorizontalAlignment="Center" Template="{StaticResource RadioButtonControlTemplate}" />
</Viewbox>

I also added a style when the radiobutton is turned off or on

<DrawingBrush x:Key="RadioButtonOff" Stretch="Uniform">
    <DrawingBrush.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Geometry="F1 M 224,3.79999C 345.889,3.79999 444.7,102.611 444.7,224.5C 444.7,346.389 345.889,445.2 224,445.2C 102.111,445.2 3.30006,346.389 3.30006,224.5C 3.30006,102.611 102.111,3.79999 224,3.79999 Z ">
                    <GeometryDrawing.Pen>
                        <Pen Thickness="3.312" LineJoin="Round" Brush="#FF5C5C5C"/>
                    </GeometryDrawing.Pen>
                    <GeometryDrawing.Brush>
                        <RadialGradientBrush RadiusX="1.67044" RadiusY="1.67044" Center="0.5,0.5" GradientOrigin="0.5,0.5">
                            <RadialGradientBrush.GradientStops>
                                <GradientStop Color="#FFB9C3C6" Offset="0.253054"/>
                                <GradientStop Color="#FF5C6163" Offset="0.286441"/>
                                <GradientStop Color="#FF000000" Offset="0.329843"/>
                            </RadialGradientBrush.GradientStops>
                            <RadialGradientBrush.RelativeTransform>
                                <TransformGroup/>
                            </RadialGradientBrush.RelativeTransform>
                        </RadialGradientBrush>
                    </GeometryDrawing.Brush>
                </GeometryDrawing>
                <GeometryDrawing Geometry="F1 M 224.333,34.5277C 328.991,34.5277 413.833,119.37 413.833,224.028C 413.833,328.685 328.991,413.527 224.333,413.527C 119.675,413.527 34.8332,328.685 34.8332,224.028C 34.8332,119.37 119.675,34.5277 224.333,34.5277 Z ">
                    <GeometryDrawing.Pen>
                        <Pen Thickness="1.44" LineJoin="Round" Brush="#FF545454"/>
                    </GeometryDrawing.Pen>
                    <GeometryDrawing.Brush>
                        <RadialGradientBrush RadiusX="0.958628" RadiusY="0.958628" Center="0.232275,0.223043" GradientOrigin="0.232275,0.223043">
                            <RadialGradientBrush.GradientStops>
                                <GradientStop Color="#FFE5EEF0" Offset="0.150862"/>
                                <GradientStop Color="#FF72797B" Offset="0.5"/>
                                <GradientStop Color="#FF000406" Offset="0.909483"/>
                            </RadialGradientBrush.GradientStops>
                            <RadialGradientBrush.RelativeTransform>
                                <TransformGroup/>
                            </RadialGradientBrush.RelativeTransform>
                        </RadialGradientBrush>
                    </GeometryDrawing.Brush>
                </GeometryDrawing>
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingBrush.Drawing>
</DrawingBrush>
<DrawingBrush x:Key="RadioButtonOn" Stretch="Uniform">
    <DrawingBrush.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Geometry="F1 M 224.3,4C 346.189,4 445,102.811 445,224.7C 445,346.589 346.189,445.4 224.3,445.4C 102.411,445.4 3.60013,346.589 3.60013,224.7C 3.60013,102.811 102.411,4 224.3,4 Z ">
                    <GeometryDrawing.Pen>
                        <Pen Thickness="3.312" LineJoin="Round" Brush="#FF5C5C5C"/>
                    </GeometryDrawing.Pen>
                    <GeometryDrawing.Brush>
                        <RadialGradientBrush RadiusX="1.67044" RadiusY="1.67044" Center="0.5,0.5" GradientOrigin="0.5,0.5">
                            <RadialGradientBrush.GradientStops>
                                <GradientStop Color="#FFB9C3C6" Offset="0.253054"/>
                                <GradientStop Color="#FF5C8A99" Offset="0.286441"/>
                                <GradientStop Color="#FF00516A" Offset="0.329843"/>
                            </RadialGradientBrush.GradientStops>
                            <RadialGradientBrush.RelativeTransform>
                                <TransformGroup/>
                            </RadialGradientBrush.RelativeTransform>
                        </RadialGradientBrush>
                    </GeometryDrawing.Brush>
                </GeometryDrawing>
                <GeometryDrawing Geometry="F1 M 224.633,34.7277C 329.291,34.7277 414.133,119.57 414.133,224.228C 414.133,328.885 329.291,413.727 224.633,413.727C 119.975,413.727 35.1333,328.885 35.1333,224.228C 35.1333,119.57 119.975,34.7277 224.633,34.7277 Z ">
                    <GeometryDrawing.Pen>
                        <Pen Thickness="1.44" LineJoin="Round" Brush="#FF545454"/>
                    </GeometryDrawing.Pen>
                    <GeometryDrawing.Brush>
                        <RadialGradientBrush RadiusX="0.958628" RadiusY="0.958628" Center="0.232275,0.223043" GradientOrigin="0.232275,0.223043">
                            <RadialGradientBrush.GradientStops>
                                <GradientStop Color="#FFE5EEF0" Offset="0.150862"/>
                                <GradientStop Color="#FF729FAE" Offset="0.5"/>
                                <GradientStop Color="#FF00516A" Offset="0.909483"/>
                            </RadialGradientBrush.GradientStops>
                            <RadialGradientBrush.RelativeTransform>
                                <TransformGroup/>
                            </RadialGradientBrush.RelativeTransform>
                        </RadialGradientBrush>
                    </GeometryDrawing.Brush>
                </GeometryDrawing>
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingBrush.Drawing>
</DrawingBrush>

There is the result

Upvotes: 0

Ray Burns
Ray Burns

Reputation: 62919

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.

Upvotes: 18

itowlson
itowlson

Reputation: 74802

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>

Upvotes: 1

Related Questions