[SOLVED] How do i add button style to window resources in wpf

Issue

I have a sliding button that has a style, inside it a content template to remove that deffault mouseover hover effect, and an image background .I want to use all that on other buttons so i want to put it in my windows.resources, but when i try i keeps giving me all kinds of errors. Could someone help me?

Here’s the code:

<Button x:Name="button1"  HorizontalAlignment="Left" Margin="-352.388,147.206,0,136.193">
        <Button.Background>
            <ImageBrush ImageSource="button1.png"/>
        </Button.Background>
        <Button.RenderTransform>
            <TranslateTransform />
        </Button.RenderTransform>
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Width" Value="508"/>
                <Setter Property="Height" Value="138"/>
                <Setter Property="UseLayoutRounding" Value="True"/>
                <Setter Property="ClipToBounds" Value="True"/>
                <Setter Property ="Background">
                    <Setter.Value>
                        <ImageBrush ImageSource ="button.png "/>
                    </Setter.Value>
                </Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">
                            <Border Name="border" BorderThickness="0" BorderBrush="Black" Background="{TemplateBinding Background}">
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Opacity" Value="0.8" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <EventTrigger RoutedEvent="MouseEnter">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetProperty="(Image.RenderTransform).(TranslateTransform.X)" From="0" To="190"  Duration="0:0:0.2" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="MouseLeave">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetProperty="(Image.RenderTransform).(TranslateTransform.X)" From="190" To="0"  Duration="0:0:0.3" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

Solution

In your Window, put the style inside like this:

<Window.Resources>
        <ResourceDictionary>
            <Style x:Key="SlidingButtonStyle" TargetType="{x:Type Button}">
                <Setter Property="Width" Value="508" />
                <Setter Property="Height" Value="138" />
                <Setter Property="UseLayoutRounding" Value="True" />
                <Setter Property="ClipToBounds" Value="True" />
                <Setter Property ="Background">
                    <Setter.Value>
                        <ImageBrush ImageSource ="button.png "/>
                    </Setter.Value>
                </Setter>
                /*********rest of code*********/
            </Style>
        </ResourceDictionary>
    </Window.Resources>

Then you can use the x:Key you specified for the style, in this case SlidingButtonStyle, to style a button like this:

<Button
    Style="{StaticResource SlidingButtonStyle}"
    /*****rest of code****/>
</Button>

For better organization, you can make another file as a ResourceDictionary which can contain multiple styles. Then you can include it in a Window or UserControl like this:

<Window.Resources>
    <ResourceDictionary Source="WindowResources.xaml" />
</Window.Resources>

Answered By – Nebeski

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *