Eric Obermuller
Eric Obermuller

Reputation: 245

Button Style is not applying XAML

So I have been learning WPF for a little while now and I am starting to use styles to make my forms look a little better.

The issue I am running into is for some reason my button style will not be applied anywhere. I am pretty sure I am overwriting the default button style. All of my other styles are working just fine I just can't figure this one out. Here is my code.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:Employee_Time_Entry">

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Colors.xaml" />
    <ResourceDictionary Source="Fonts.xaml" />
    <ResourceDictionary Source="Texts.xaml" />
</ResourceDictionary.MergedDictionaries>

<!-- Regular button -->
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseStyle}">

    <Setter Property="Background" Value="{StaticResource BackgroundOrangeBrush}" />
    <Setter Property="Foreground" Value="{StaticResource ForegroundLightBrush}" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="FontSize" Value="{StaticResource FontSizeLarge}" />
    <Setter Property="FontFamily" Value="{StaticResource LatoRegular}" />
    <Setter Property="Padding" Value="50 10" />
    <Setter Property="Margin" Value="0 10" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ButtonBase}">
                <Border x:Name="border"
                        CornerRadius="10"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}" 
                        SnapsToDevicePixels="True">
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Here is the form code where the button will not apply the style.

<Page x:Class="Employee_Time_Entry.Views.Login"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:Employee_Time_Entry"
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="500"
  Title="Login">

<Border>
    <Border.Background>
        <ImageBrush ImageSource="/Backgrounds/BlueWaveBackground.jpg"/>
    </Border.Background>
    <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" HorizontalAlignment="Center">
            <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" TextBlock.TextAlignment="Center" >
                <Border Background="{StaticResource ForegroundLightBrush}" 
                        CornerRadius="10" 
                        Padding="15 10 15 15" 
                        Width="250" 
                        Margin="50 50 50 0">
                    <StackPanel>
                        <TextBlock Text="Sign In" Padding="0 0 0 10" FontSize="{StaticResource FontSizeLarge}" FontFamily="{StaticResource LatoBold}"/>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid Grid.Column="0">
                                <StackPanel>
                                    <TextBlock HorizontalAlignment="Left" Margin="0 10 5 0" Text="User Name:" Style="{StaticResource DefaultTextBox}"/>
                                    <TextBlock HorizontalAlignment="Left" Margin="0 15 5 0" Text="Password:" Style="{StaticResource DefaultTextBox}"/>
                                </StackPanel>
                            </Grid>
                            <Grid Grid.Column="1">
                                <StackPanel>
                                    <TextBox/>
                                    <PasswordBox/>
                                    <Button Content="Login" 
                                            Margin = "10 10"/>
                                </StackPanel>
                            </Grid>
                        </Grid>
                    </StackPanel>
                </Border>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</Border>

Here is a picture of my form

Upvotes: 0

Views: 571

Answers (2)

Rjosh
Rjosh

Reputation: 11

Bind the style to your button, Like this

<Button Style="{StaticResource /the name of your style here/}" Content="Login" Margin = "10 10"/>

On your button style

<Style TargetType="{x:Type Button}" x:Key="/nameyourstyle/" BasedOn="{StaticResource BaseStyle}">

.....

Upvotes: 0

Shawn Kendrot
Shawn Kendrot

Reputation: 12465

You need to make sure the resource file is referenced either application wide or within the page you want to apply it.

To apply the resources from the file to a specific page, you need to add it to the page resources.

<Page.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Assembly.Namespace;component/MyResourceFileName.xaml"
                                x:Name="Dict" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Page.Resources>

To apply the resource to your entire application, you would do the same but to your app.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Assembly.Namespace;component/MyResourceFileName.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

It should be noted that your button style will not display any content. Your style only has a Border which cannot display content. Make sure you add a ContentPresenter inside of the Button

Upvotes: 1

Related Questions