在 WPF 中为所有窗口中的按钮应用样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16530184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:47:32  来源:igfitidea点击:

Apply style for buttons in all Windows in WPF

wpfxamlbuttonstyles

提问by PiousVenom

I have a style setup in my XAML for the creation of round corner Buttons in my WPF window. I would like this style to apply to all buttons on all windows in my application.

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

Is there a way, similar to CSS, that I could put this into another file and reference it somehow in all my windows? Or do I just need to copy and paste it every time.

有没有办法,类似于 CSS,我可以把它放到另一个文件中,并在我的所有窗口中以某种方式引用它?或者我只需要每次都复制和粘贴它。

回答by Sonhja

If you want to do it in a clean way, you can create a ResourceDictionary.xaml, that has the same funcion that CSSin Web design.

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

First, go to your project and add a ResourceDictionary. Inside it you can add styles for all the desired elements you want, for example, change color background of a Buttonthat applies to all your buttons:

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

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

If you don't specify an identifier on each Style, that style will apply to all controls that match with the TargetTypeyou specified. If you want button to look different, you can do the same as above, but also include an identifier to that style, that will be used by each different button:

如果您没有在 each 上指定标识符Style,则该样式将应用于与TargetType您指定的匹配的所有控件 。如果您希望按钮看起来不同,您可以执行与上述相同的操作,但还要包含该样式的标识符,该标识符将被每个不同的按钮使用:

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

Then, on each .xamlthat you want styles to be applied you have to add the reference to this ResourceDictionary.xamlthat you are creating:

然后,在.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>

I think this is what you are looking for.

我想这就是你要找的。

If you want to draw a rounded button, you need to override the Templateproperty of the button. This means that you need to tell button every action he needs to do from the moment you override it. See here. So, in a little and reduced concept, you would like to write something like this:

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

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

See that here I override all basic properties needed to draw a basic funcitonal button, like Foreground, Background, Width... and the MouseOverevent, to change colour when passing mouse over it. The CornerRadiusproperty of the Borderinside ControlTemplateis the radius you are looking for.

看到这里我覆盖了绘制基本功能按钮所需的所有基本属性,例如Foreground, Background, Width... 和MouseOver事件,以在鼠标经过时更改颜色。里面的CornerRadius属性就是你要找的半径。BorderControlTemplate

So basically, you are overriding the border property that comes by default for all buttons.

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

回答by Gauthier G. Letellier

You could use the application resources to do that.

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

Here's a bit of code for example (in app.xaml)

例如,这里有一些代码(在 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>

and then, for your buttons (for example):

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

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

Hope this will help you to find what you're looking for.

希望这会帮助你找到你要找的东西。

回答by Jeff

Google Reference Dictionary. You can put all your styles in there. then, just add a "reference" to it in your window/user control.

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

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

Now, all styles in the above referenced XAML file, will be applied to all objects in the user control.

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

回答by Bibaswann Bandyopadhyay

The easiest way according to me is:

根据我的说法,最简单的方法是:

  1. Right click on a button in design surface

  2. Choose Edit Template->Edit a Copy

  3. Choose "Define in Application" radio button

  4. The Style will be created in App.xaml file

  5. Add that resource to every button using "style" tag

  1. 右键单击设计图面上的按钮

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

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

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

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