按钮样式未应用XAML

时间:2018-02-12 14:54:35

标签: c# wpf xaml

所以我一直在学习WPF一段时间,我开始使用样式让我的表单看起来好一点。

我遇到的问题是由于某种原因我的按钮样式不会在任何地方应用。我很确定我会覆盖默认的按钮样式。我所有的其他风格都运作得很好我只是想不出这个。这是我的代码。

<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>

这是按钮不会应用样式的表单代码。

<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

2 个答案:

答案 0 :(得分:1)

您需要确保在应用程序范围内或在要应用它的页面中引用资源文件。

要将文件中的资源应用到特定页面,您需要将其添加到页面资源中。

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

要将资源应用于整个应用程序,您可以对app.xaml

执行相同的操作
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Assembly.Namespace;component/MyResourceFileName.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

应该注意,您的按钮样式不会显示任何内容。您的样式只有一个无法显示内容的边框。确保在Button

中添加ContentPresenter

答案 1 :(得分:0)

将样式绑定到按钮,就像这样

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

按您的按钮样式

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

.....