Use the same definitions for the two control patterns.

I have a definition of some visual elements that I want to use for two different controls (Button, Thumb). Is there a way to get rid of duplicate code?

   <Style x:Key="HorizontalSliderThumbStyle" TargetType="{x:Type Thumb}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Thumb}">
this block is the same -->  <Grid>
                                <VisualStateManager.VisualStateGroups>
                                   ...
                                </VisualStateManager.VisualStateGroups>
                                <Rectangle x:Name="borderRect" Fill="{TemplateBinding BorderBrush}" />
                                   ...


    <Style x:Key="KeyboardButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
this block is the same -->  <Grid>
                                <VisualStateManager.VisualStateGroups>
                                    ...
                                </VisualStateManager.VisualStateGroups>
                                <Rectangle x:Name="borderRect" Fill="{TemplateBinding BorderBrush}" />
                                   ...
+4
source share
3 answers

You can do something like this:

<ControlTemplate TargetType="{x:Type Control}" x:Key="SharedControlTemplate">
    <Grid>
        <Rectangle Fill="{TemplateBinding BorderBrush}"/>
        <TextBlock Text="Shared Control Template"/>
    </Grid>
</ControlTemplate>

<Style TargetType="{x:Type Thumb}" x:Key="HorizontalSliderThumbStyle">
    <Setter Property="Template" Value="{StaticResource SharedControlTemplate}"/>
</Style>

<Style TargetType="{x:Type Button}" x:Key="KeyboardButtonStyle">
    <Setter Property="Template" Value="{StaticResource SharedControlTemplate}"/>
</Style>

Both Thumband Buttonobtained from the base class Control(not once, but is one of the ancestors). Therefore, if you define for it ControlTemplate, you can specify both controls to the same template. Then, in style, you will adapt your individual behavior if you need it.

Usage example:

<Thumb Style="{StaticResource HorizontalSliderThumbStyle}" BorderBrush="AliceBlue"/>
<Button Style="{StaticResource KeyboardButtonStyle}" BorderBrush="Aquamarine"/>

enter image description here

EDIT:

, , ContentPresenter ControlTemplate. Content ContentControl. Control ContentControl, Controls, ... , . :

<ControlTemplate TargetType="{x:Type Control}" x:Key="SharedControlTemplate">
    <Grid>
        <Rectangle Fill="{TemplateBinding BorderBrush}"/>
        <TextBlock Text="Shared Control Template"/>
        <ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
    </Grid>
</ControlTemplate>

, ContentPresenter, a ContentControl. - , :

<Thumb Style="{StaticResource HorizontalSliderThumbStyle}" BorderBrush="AliceBlue"/>
<Button Style="{StaticResource KeyboardButtonStyle}" BorderBrush="Aquamarine">
    <Rectangle Fill="Red" Width="100" Height="20"/>
</Button>

enter image description here

+2

DataTemplate , ContentControl Class, ControlTemplate. :

Resources:

<DataTemplate x:Key="InnerContent">
    <!-- Define your inner content here -->
</DataTemplate>

...

<ControlTemplate TargetType="{x:Type Thumb}">
    <ContentControl Content="{Binding}" ContentTemplate="{StaticResource InnerContent}" />
</ControlTemplate>

...

<ControlTemplate TargetType="{x:Type Button}">
    <ContentControl Content="{Binding}" ContentTemplate="{StaticResource InnerContent}" />
</ControlTemplate>

, , ContentPresenter .

0

You can create a basic style.

<Style x:Key="BaseStyle" TargetType="Control">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Control">
                 ...
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

and extract your style from it with BasedOn

<Style x:Key="ButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseStyle}">
        ...        
</Style>
0
source

All Articles