views:

410

answers:

2

Hello, I am currently trying to figure out how to reuse a template in other controltemplates (as the title says). What I am trying to do is make a bunch of buttons that are all slightly different but have several similar features. They all share several of the same graphical elements and have the same triggers that deal with those graphical elements. What I am hoping to do is be able to take that code out and put it into another template and just have all of the buttons reference to that template. That way it is easier to manager and just makes more sense overall. I'm sure there is a way but I'm still kind of new to WPF. Thanks for any help!

EDIT: Here is some code showing what I want to do.

<ControlTemplate x:Key="LeftJustifyButtonTemplate" TargetType="{x:Type RadioButton}">
    <Grid Width="24" Height="24">
        <Rectangle HorizontalAlignment="Stretch" Fill="#00000000" Stroke="{x:Null}"/>
        <Rectangle x:Name="backRectangle" HorizontalAlignment="Stretch" Margin="0,0,0,0" Stroke="#FFB9B9B9" StrokeThickness="0.5" RadiusX="4" RadiusY="4" Visibility="Hidden">
            <Rectangle.Fill>
                <LinearGradientBrush EndPoint="0.146,0.146" StartPoint="2.057,2.057">
                    <GradientStop Color="#FF000000" Offset="0"/>
                    <GradientStop Color="#FFFFFFFF" Offset="1"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
        <Rectangle x:Name="foreRectangle" Margin="1,1,1,1" VerticalAlignment="Stretch" Fill="#FFE0E0E0" Stroke="{x:Null}" StrokeThickness="0.5" RadiusX="4" RadiusY="4" Visibility="Hidden"/>
        <Path Margin="2.875,7,2.875,0" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M2.875,7.5 L21.145964,7.5" VerticalAlignment="Top" Height="1" StrokeThickness="0.5"/>
        <Path Margin="2.875,9.375,8,0" VerticalAlignment="Top" Height="1" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M3.625,4 L17.514069,4" StrokeThickness="0.5"/>
        <Path Margin="2.875,11.5,2.875,11.5" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M2.875,7.5 L21.145964,7.5" StrokeThickness="0.5"/>
        <Path Margin="2.875,0,8,9.375" VerticalAlignment="Bottom" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M3.625,4 L17.514069,4" Height="1" StrokeThickness="0.5"/>
        <Path Margin="2.875,0,2.875,7" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Data="M2.875,7.5 L21.145964,7.5" VerticalAlignment="Bottom" Height="1" StrokeThickness="0.5"/>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter TargetName="backRectangle" Property="Visibility" Value="Visible"/>
            <Setter TargetName="foreRectangle" Property="Visibility" Value="Visible"/>
        </Trigger>
        <Trigger Property="IsChecked" Value="True">
            <Setter TargetName="backRectangle" Property="Visibility" Value="Visible"/>
            <Setter TargetName="foreRectangle" Property="Visibility" Value="Visible"/>
            <Setter TargetName="foreRectangle" Property="Fill" Value="#FFFFFFFF"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

Pretty much all of that, except for the 5 Paths in the middle, is used in several buttons. This is because it is used for the mouse over and checked triggers. What I want to do is be able to have all of that code be in one place and then have several buttons be able to reference that code.

A: 

What kind of difference exactly are you talking about? You might be able to pull it off with a combination of attached properties and triggers, but we'd need to know more about your problem.

Denis Troller
A: 

I don't believe you can do this with templates, but check out the BasedOn property of the Style.

Paul Betts