joozek
joozek

Reputation: 2191

How to create parametrized style in xaml without custom control

I have two styles, which are almost identical:

<Style x:Key="CancelButtonStyle" TargetType="{x:Type Button}" >
    <!-- gigantic common code -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <!-- more common code -->
                <Path Data="DIFFERENT_VALUE_A"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="OkButtonStyle" TargetType="{x:Type Button}" >
    <!-- gigantic common code -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <!-- more common code -->
                <Path Data="DIFFERENT_VALUE_B"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I want to extract the common code ("style template"), and then use it like that:

<Style x:Key="OkButtonStyle" Base="PathButtonStyle" PathData="DIFFERENT_VALUE_B" />

EDIT: As I specified in title, I don't want to create my own control

Upvotes: 1

Views: 135

Answers (3)

Benoit Catherinet
Benoit Catherinet

Reputation: 3345

You could use Tag to pass the PathData:

<Style x:Key="BaseStyle" TargetType="{x:Type Button}" >
    <!-- gigantic common code -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <!-- more common code -->
                <Path Data="{TemplateBinding Tag}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

and then set the Tag in your individual Style:

<Style x:Key="CancelButtonStyle" TargetType="{x:Type Button}" 
    BasedOn="{StaticResource BaseStyle}">
    <Setter Property="Tag" Value="MY PAth A" />
</Style>

<Style x:Key="OkButtonStyle" TargetType="{x:Type Button}"
    BasedOn="{StaticResource BaseStyle}">
    <Setter Property="Tag" Value="MY PAth B" />
</Style>

Upvotes: 1

Sheridan
Sheridan

Reputation: 69959

Just define your ControlTemplate objects separately in Resources:

<ControlTemplate x:Key="ControlTemplate1">
    <!-- different part -->
</ControlTemplate>

<ControlTemplate x:Key="ControlTemplate2">
    <!-- different part -->
</ControlTemplate>

Create one Style for the common parts:

<Style x:Key="BaseStyle" TargetType="{x:Type Button}" >
    <!-- gigantic common code -->
</Style>

Then base your new styles on that one, referencing your new ControlTemplates:

<Style x:Key="CancelButtonStyle" TargetType="{x:Type Button}" 
    BasedOn="{StaticResource BaseStyle}">
    <Setter Property="Template" Value="{StaticResource ControlTemplate1}" />
</Style>

<Style x:Key="OkButtonStyle" TargetType="{x:Type Button}"
    BasedOn="{StaticResource BaseStyle}">
    <Setter Property="Template" Value="{StaticResource ControlTemplate2}" />
</Style>

Upvotes: 3

Florian Gl
Florian Gl

Reputation: 6014

You can create your own Button, which derives from the Wpf-Button and has the property PathData:

public class PathButton:Button
{
    public string PathData {get;set;}
}

The your style could be like:

<Style x:Key="PathButtonStyle" TargetType="{x:Type ns:PathButton}" >
    <!-- gigantic common code -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <!-- more common code -->
                <Path Data="{TemplateBinding PathData}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

To use it you have to do this:

<ns:PathButton Style="{StaticResource PathButtonStyle}" PathData="M200 200 100 100 L0 0"/>

Or you create your own Custom Control, the way would be the same.

The adventage here is no matter how many different buttons you want to use, there always will be just one Style in your resources and one class (PathButton). You just have to change the PathData-property of your PathButton to change it's appereance.

Upvotes: 0

Related Questions