繁体   English   中英

按钮样式未应用XAML

[英]Button Style is not applying 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>

这是我表格的图片

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

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

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

要将资源应用到整个应用程序,您可以执行相同的操作,但要应用到您的应用程序。

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

请注意,您的按钮样式将不会显示任何内容。 您的样式只有一个边框,无法显示内容。 确保在按钮内添加ContentPresenter

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

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

.....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM