tags:

views:

65

answers:

3

For example, I have created a button with a check mark drawn on it. I need to use it in a couple places in my window.

<Button Width="25">
    <!-- Draw a Green checkmark -->
    <Polyline Points="2,5,6,10,13,1" Stroke="Green" StrokeThickness="4" StrokeLineJoin="Round" StrokeStartLineCap="Round" StrokeEndLineCap="Round" />
</Button>

Since I only want to change the content, I'm not sure if Styles or Templates apply. Do I need to create a UserControl?

Edit: This can be accomplished with a style. But, I only want to change the content and I don't want to override the current style.

+2  A: 

Create a UserControl with the contents of the button you created if you want to have some sort of dynamic capability to it i.e. add new properties. If you just have a button that just has the visual appearance to have a check then I'd use a style.

Craig Suchanec
+1  A: 

This is crying out for a style. Add the following Style to your Window.Resources:

<style TargetType="Button" x:Key="tick">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Polyline Points="2,5,6,10,13,1" Stroke="Green" StrokeThickness="4" StrokeLineJoin="Round" StrokeStartLineCap="Round" StrokeEndLineCap="Round" />
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

and you can then define your button as:

<Button Width="25" Style="{StaticResource tick}" />
Leom Burke
This particular style meets the requirement, but it discards any Content supplied for the Button using the Style. It's a good solution, however as you can always modify the template to include a ContentPresenter later. For instance, you could create a horizontal StackPanel with the Polyline followed by the ContentPresenter and now you could optionally append text (or any other content for that matter) to any 'tick' style button.
Dan Bryant
That is a very good point - I did work to rule on it. Adding a content presenter would be a nice feature.
Leom Burke
Thanks. But my whole application already has styles applied to it. When I apply this style I loose all that. Is there a way around this?
TheSean
@TheSean, You can use the BasedOn property of the Style to use another style as the base. See: http://msdn.microsoft.com/en-us/library/system.windows.style.basedon.aspx
Dan Bryant
I don't know the theme at design time, so it would need to be dynamic. From what I've read, that doesn't seem possible.
TheSean
@TheSean, check out the style based on the default style for the control type. i.e. `BasedOn="{StaticResource {x:Type Button}}"`
Dan Bryant
I think you actually need to use BasedOn="{DynamicResource {x:Type Button}}" if the style can change at run time as he was saying. I think that will work.
Craig Suchanec
@Craig: Thanks, but this doesn't fly.A 'DynamicResourceExtension' cannot be set on the 'BasedOn' property of type 'Style'. A 'DynamicResourceExtension' can only be set on a DependencyProperty of a DependencyObject.
TheSean
@TheSean It would appear the only recourse would be to make a user control containing the button with the check content. That's not a great solution but I can't think of any other way to get around the problem.
Craig Suchanec
A: 

Create a UserControl with the contents of the button you created. The easiest option to create the user control is to use Expression Blend. Select all of our controls to whom u want to make UserControl. Right Click and Select "make into Usercontrol". Thats it. U can use this control anywhere in your project just by drag and drop.

Hope it will help u,

Saghar Ayyaz

Saghar