JKennedy
JKennedy

Reputation: 18799

How to base a style on an inherited style

So I have a Master Detail page. And in my Master page I have a style that will set the style for all buttons.

Master Page

 <Style TargetType="Button">
            <Setter Property="Background" Value="White" />
            <Setter Property="TextBlock.TextAlignment" Value="Center" />
            <Setter Property="RenderTransformOrigin" Value="-0.893,-11.7"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>

in my Detail Page I have a button where I would like to add some triggers to through the style

            <Button Content="{Binding DeleteMultipleButton}" Margin="0,0,5,0" CommandParameter="{Binding ElementName=files, Path=SelectedItems}" Command="{Binding DeleteMultipleFiles}">
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <Setter Property="IsEnabled" Value="True"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Deleting}" Value="True">
                                <Setter Property="IsEnabled" Value="False"/>
                            </DataTrigger>                              
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>

But this overwrites the inherited style and I lose my button styling. Is there a way I can base my button style on the inherited style? (I can't do this through static resource and a key because the resource is in a different page)

Upvotes: 2

Views: 151

Answers (2)

JKennedy
JKennedy

Reputation: 18799

So Because the two styles were on different pages it makes inheriting from the default style impossible so what I did was build an App.xaml resource file with my style in it and then on both pages add the code:

    <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Resource.xaml" />
    </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

to my xaml. This meant that my page was then able to override the inherited style by using Mike Eason's code:

<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">

Upvotes: 0

Mike Eason
Mike Eason

Reputation: 9713

You can use the BasedOn property:

<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">

Keep your base button style as it is:

<Style TargetType="Button">
        <Setter Property="Background" Value="White" />
        <Setter Property="TextBlock.TextAlignment" Value="Center" />
        <Setter Property="RenderTransformOrigin" Value="-0.893,-11.7"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>

And apply the BasedOn property to the button on the Detail Page:

<Button>
        <Button.Style>
            <Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
                ...
            </Style>
        </Button.Style>
    </Button>

Upvotes: 2

Related Questions