WPF Reusing a Template in Other ControlTemplates

I'm currently trying to figure out how to reuse a template in other controltemplates (as the name says). What I'm trying to do is create a bunch of buttons that are slightly different from each other, but have several similar functions. They all have several identical graphic elements and have the same triggers that deal with these graphic elements. I hope that I can extract this code and put it in another template, and just all the buttons link to this template. Thus, it is easier for the manager and just makes sense in general. I'm sure there is a way, but I'm still new to WPF. Thanks for any help!

EDIT: Here is the code that shows 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>

, 5 , . , . , , .

+3
2

, ? , , .

0

, , BasedOn .

0

Source: https://habr.com/ru/post/1705511/


All Articles