【问题标题】:Apply style for buttons in all Windows in WPF在 WPF 中为所有 Windows 中的按钮应用样式
【发布时间】:2023-04-06 16:40:02
【问题描述】:

我在我的 XAML 中有一个样式设置,用于在我的 WPF 窗口中创建圆角 Buttons。我希望这种样式适用于我的应用程序中所有窗口上的所有按钮。

有没有一种类似于 CSS 的方法,可以将它放入另一个文件并在我的所有窗口中以某种方式引用它?还是我每次都需要复制粘贴。

【问题讨论】:

    标签: wpf xaml button styles


    【解决方案1】:

    如果你想以一种干净的方式来做,你可以创建一个ResourceDictionary.xaml,它与网页设计中的CSS具有相同的功能。

    首先,转到您的项目并添加ResourceDictionary。在其中,您可以为所需的所有元素添加样式,例如,更改适用于所有按钮的Button 的颜色背景:

    // Base style for all buttons
    <Style TargetType="Button">
        <Setter Property="Background" Value="Red" />
    </Style>
    

    如果您未在每个 Style 上指定标识符,则该样式将应用于与 TargetType 你指定的。如果您希望按钮看起来不同,您可以执行与上述相同的操作,但还包括该样式的标识符,每个不同的按钮都将使用该标识符:

    // Specific style for blue buttons
    <Style TargetType="Button" x:Key="BlueButton">
        <Setter Property="Background" Value="Blue" />
    </Style>
    

    然后,在您希望应用样式的每个 .xaml 上,您必须添加对您正在创建的 ResourceDictionary.xaml 的引用:

    <Window.... >
        <Window.References>
            <ResourceDictionary>
               <ResourceDictionary Source="MyResourceDictionary.xaml" />
            </ResourceDictionary>
        </Window.References>
    
        <Grid>
           <Button Content="Button with red background" />
           <Button Style="{StaticResource BlueButton}" Content="Button with blue background" />
        </Grid>
    </Window>
    

    我想这就是你要找的。​​p>

    如果要绘制圆形按钮,则需要覆盖按钮的Template 属性。这意味着从您覆盖它的那一刻起,您需要告诉按钮他需要执行的每个操作。见here。所以,在一个小而简化的概念中,你想写这样的东西:

    <Style TargetType="Button">
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Background" Value="DarkBlue" />
        <Setter Property="Width" Value="150" />
        <Setter Property="Height" Value="35" />
        <Setter Property="FontSize" Value="16" />
        <Setter Property="FontFamily" Value="Calibri" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="{TemplateBinding Background}"
                        BorderBrush="LightBlue" BorderThickness="1" CornerRadius="15,0,15,0" x:Name="bd">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                        Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="bd" Property="Background" Value="LightGray"/>
                            <Setter Property="Foreground" Value="White" />
                            <Setter Property="Cursor" Value="Hand" />
                        </Trigger>
    
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    看到这里我覆盖了绘制基本功能按钮所需的所有基本属性,例如ForegroundBackgroundWidth... 和MouseOver 事件,以便在鼠标经过时更改颜色。 ControlTemplate 内的BorderCornerRadius 属性是您要查找的半径。

    所以基本上,您将覆盖所有按钮默认提供的边框属性。

    【讨论】:

    • 我没有“”属性。我错过了什么?
    • 我也错过了Window.References,我使用了Funk's Answer here,这就成功了。杰夫在下面的回答似乎非常相似,但我猜仅限于控件。
    【解决方案2】:

    您可以使用应用程序资源来做到这一点。

    这里有一些代码示例(在 app.xaml 中)

    <Application.Resources>
      <Style TargetType="Button" x:Key="GelButton" >
         <Setter Property="Margin" Value="1,2,1,2"/>
         <Setter Property="HorizontalAlignment" Value="Left"/>
      </Style>
    </Application.Resources>
    

    然后,对于您的按钮(例如):

    <Button Height="50" Width="250" Style="{StaticResource GelButton}" Content="Button 1" />
    <Button Height="50" Width="250" Style="{StaticResource GelButton}" Content="Button 2" />
    

    希望这将帮助您找到所需的内容。

    【讨论】:

    • 如果您在样式定义中省略了x:Key,那么您不需要将样式应用于每个按钮。 (请参阅@sonhja 的答案。)
    • 谢谢,我正在寻找那一点点!
    • 是的,OP 要求设置“所有按钮”的样式,而不仅仅是应用命名样式的按钮。想象一下,必须将Style 属性应用于您需要的每个按钮。
    【解决方案3】:

    谷歌参考词典。你可以把你所有的风格放在那里。然后,只需在您的窗口/用户控件中添加一个“引用”即可。

        <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/SialTPChat.UI.Design;component/Styles/Styles.xaml" />
        </ResourceDictionary>
        </UserControl.Resources>
    

    现在,上面引用的 XAML 文件中的所有样式都将应用于用户控件中的所有对象。

    【讨论】:

      【解决方案4】:

      我认为最简单的方法是:

      1. 右键单击设计界面中的按钮

      2. 选择编辑模板->编辑副本

      3. 选择“在应用程序中定义”单选按钮

      4. 样式将在 App.xaml 文件中创建

      5. 使用“样式”标签将该资源添加到每个按钮

      【讨论】:

        猜你喜欢
        • 2017-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-29
        • 2018-07-14
        相关资源
        最近更新 更多