Set styles in XAML

I've got the following base Style in XAML and I need to create a few new styles based on it with only 1 property different in each new style.

<UserControl.Resources>
<Style x:Key="BaseButtonStyle" TargetType="{x:Type ButtonBase}">
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Background" Value="{StaticResource Button.Background}"/>
            <Setter Property="BorderBrush" Value="{StaticResource Button.BorderBrush}"/>
            <Setter Property="Foreground" Value="{StaticResource Button.Foreground}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="10,1,10,1"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ButtonBase}">
                        <Grid x:Name="Root" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                            <Grid x:Name="FocusState" Opacity="0">
                                <Border CornerRadius="4" Style="{StaticResource FocusTemplate}"/>
                            </Grid>
                            <Grid x:Name="DefaultState">
                                <Border Background="{StaticResource Button.DefaultState.Border1}"
                                    Padding="1" CornerRadius="3">
                                    <Border Background="{StaticResource Button.DefaultState.Border2}"
                                        Padding="1" CornerRadius="2">
                                    </Border>
                                </Border>
                            </Grid>
                            <Grid x:Name="HoverState" Opacity="0">
                                <Border Background="{StaticResource Button.HoverState.Border1}"
                                    Padding="1" CornerRadius="3">
                                    <Grid>
                                        <Border Background="{StaticResource Button.HoverState.Border2}"
                                            Padding="1" CornerRadius="2">
                                            <Border Background="{StaticResource Button.HoverState.Border3}"
                                                CornerRadius="1"/>
                                        </Border>
                                        <Border>
                                            <Border.Background>
                                                <RadialGradientBrush>
                                                    <RadialGradientBrush.RelativeTransform>
                                                        <TranslateTransform X="0" Y="0.5"/>
                                                    </RadialGradientBrush.RelativeTransform>
                                                    <GradientStop Color="#00FFFFFF" Offset="1"/>
                                                    <GradientStop Color="#BFFFFFFF" Offset="0"/>
                                                </RadialGradientBrush>
                                            </Border.Background>
                                        </Border>
                                    </Grid>
                                </Border>
                            </Grid>
                            <Grid x:Name="PressedState" Opacity="0">
                                <Border Background="{StaticResource Button.PressedState.Border1}"
                                    Padding="1" CornerRadius="3">
                                    <Border Background="{StaticResource Button.PressedState.Border2}"
                                        Padding="1" CornerRadius="2">
                                        <Border Background="{StaticResource Button.PressedState.Border3}"
                                            CornerRadius="1"/>
                                    </Border>
                                </Border>
                            </Grid>
                            <Grid x:Name="CheckedState" Opacity="0">
                                <Border Background="{StaticResource Button.CheckedState.Border1}"
                                    Padding="1" CornerRadius="3">
                                    <Border Background="{StaticResource Button.CheckedState.Border2}"
                                        Padding="1" CornerRadius="2">
                                        <Border Background="{StaticResource Button.CheckedState.Border3}"
                                            CornerRadius="1"/>
                                    </Border>
                                </Border>
                            </Grid>
                            <Grid Margin="2">
                                <ContentPresenter Margin="{TemplateBinding Padding}"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              RecognizesAccessKey="True"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Grid>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="DefaultState" Property="Opacity" Value="0"/>
                                <Setter TargetName="HoverState" Property="Opacity" Value="1"/>
                                <Setter TargetName="PressedState" Property="Opacity" Value="0"/>
                                <Setter TargetName="CheckedState" Property="Opacity" Value="0"/>
                                <Setter Property="Foreground"
                                    Value="{StaticResource Button.Foreground}"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="DefaultState" Property="Opacity" Value="0"/>
                                <Setter TargetName="HoverState" Property="Opacity" Value="0"/>
                                <Setter TargetName="PressedState" Property="Opacity" Value="1"/>
                                <Setter TargetName="CheckedState" Property="Opacity" Value="0"/>
                                <Setter Property="Foreground"
                                    Value="{StaticResource Button.Foreground}"/>
                            </Trigger>
                            <Trigger Property="ToggleButton.IsChecked" Value="True">
                                <Setter TargetName="DefaultState" Property="Opacity" Value="0"/>
                                <Setter TargetName="HoverState" Property="Opacity" Value="0"/>
                                <Setter TargetName="PressedState" Property="Opacity" Value="0"/>
                                <Setter TargetName="CheckedState" Property="Opacity" Value="1"/>
                                <Setter Property="Foreground"
                                    Value="{StaticResource Button.Foreground}"/>
                            </Trigger>
                            <Trigger Property="IsFocused" Value="True">
                                <Setter TargetName="FocusState" Property="Opacity" Value="1"/>
                            </Trigger>
                            <Trigger Property="Button.IsDefaulted" Value="True">
                                <Setter TargetName="FocusState" Property="Opacity" Value="1"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="Root" Property="Opacity" Value="0.35"/>
                                <Setter Property="Foreground"
                                    Value="{StaticResource Button.Foreground}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
</Style>

<UserControl.Resources>

and I set this as the base Button style using

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

so far so good. Next I want to have a second style with the only portion different being

<Border Background="{StaticResource Button.DefaultState.BorderNEWZ}"
//instead of
<Border Background="{StaticResource Button.DefaultState.Border2}"

so I've tried NEWSTYLETEST...

    <Style x:Key="NEWSTYLETEST" TargetType="{x:Type Button}" BasedOn="{StaticResource BaseButtonStyle}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ButtonBase}">
                    <Grid x:Name="Root" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                        <Grid x:Name="FocusState" Opacity="0">
                            <Border CornerRadius="4" Style="{StaticResource FocusTemplate}"/>
                        </Grid>
                        <Grid x:Name="DefaultState">
                            <Border Background="{StaticResource Button.DefaultState.Border1}"
                                Padding="1" CornerRadius="3">
                                <Border Background="{StaticResource Button.DefaultState.BorderNEWZ}"
                                    Padding="1" CornerRadius="2">
                                </Border>
                            </Border>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

This does change the control part I need from Button.DefaultState.Border2 to Button.DefaultState.BorderNEWZ but it omits the remainder of the values contained in the rest of <Setter Property="Template"> in the base style.

I could just copy the whole of <Setter Property="Template"> each time and change the line I need but that seems overkill. Is there a way to target only Button.DefaultState.Border2 for change in each new style whilst keep everything else the same without repeating large chunks of the code?

Answers


The BasedOn works on a per-property basis, so in this case you've told it to override the "Template" property. (all or nothing) Ultimately the answers you'll be looking for are outlined here:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/abe21d46-8e32-4f33-bb8f-750bdc38cb14/


Need Your Help

How is flush: true different from manually flushing the currentSession?

hibernate grails gorm

I have a GORM object that I'm working with in an integration test. It has a beforeUpdate hook that keeps a history of the previous password hashes. The code looks something like this:

Impossible? HTML <TD> mouseover border-color change with border-collapse?

html css

I have wish to have a table where all borders (internal/external) are a single pixel in width, I achieve this by setting the border-collapse style on the table.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.