0

I use a template for my buttons. Now I got to a point where I want to change the template on runtime. But that doesn't work with this template. The template looks like this:

<Application.Resources>
    <Style TargetType="{x:Type Button}">
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Name="Border" Background="Transparent">
                        <ContentPresenter RecognizesAccessKey="True"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter TargetName="Border" Property="BorderBrush" Value="#ffffff" />
                        </Trigger>
                        <Trigger Property="IsDefaulted" Value="true">
                            <Setter TargetName="Border" Property="BorderBrush" Value="#ffffff" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="Transparent" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="Transparent" />
                            <Setter TargetName="Border" Property="BorderBrush" Value="#ffffff" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="Border" Property="Background" Value="Transparent" />
                            <Setter TargetName="Border" Property="BorderBrush" Value="#ffffff" />
                            <Setter Property="Foreground" Value="Transparent"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Application.Resources>

I used it on different windows, therefore as application resource. How do I access this template and change it for only some buttons? I thought about creating different templates. But how do I create one where I can change things like Background, etc. on runtime?

Marcel Grüger
  • 885
  • 1
  • 9
  • 25

1 Answers1

0

If you give an x:key to your resource, you should be able to do something like this:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MyProject.Styles;component/ButtonStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

Then Just use :

<Button>
<Button.Style>
    <Style BasedOn="{StaticResource StyleFromButtonStyles}" TargetType="Button">

And override with Style.Setter the property that you want to change

Daniele Sartori
  • 1,674
  • 22
  • 38