按钮的 WPF 样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34369590/
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-13 13:35:32  来源:igfitidea点击:

WPF style for buttons

c#wpfwpf-controls

提问by HymanyBoi

I haven't used WPF for a very long time (close to 4 years). But now started to use it again for some usage, you can see one of my top asked questions is in WPF. The reason I mention is this because I do have some knowledge about WPF and how it works. My latest project invovlves using BLEND to achieve a very good UI. So here I have a Photoshop layout, and I am trying to get this layout design,

我已经很长时间(接近 4 年)没有使用 WPF。但是现在开始再次使用它进行一些用途,您可以看到我最常问的问题之一是在 WPF 中。我之所以提到这一点,是因为我确实对 WPF 及其工作原理有一些了解。我最近的项目涉及使用 BLEND 来实现一个非常好的 UI。所以这里我有一个 Photoshop 布局,我正在尝试获得这个布局设计,

enter image description here

在此处输入图片说明

And so far I only have this,

到目前为止,我只有这个,

enter image description here

在此处输入图片说明

So really would appreciate if someone could point me on the following things.

因此,如果有人能指出以下几点,我将不胜感激。

How to get the border glow effect and curves also how to group them to a particular style in a separate file and link it to the buttons. Thanks for your time and answers

如何获得边框发光效果和曲线以及如何在单独的文件中将它们分组为特定样式并将其链接到按钮。感谢您的时间和答案

EDIT: With my styles code.

编辑:使用我的样式代码。

I have the following styles code in Custom_Styles.xaml (Resource dictionary) How do I link it to the button?

我在 Custom_Styles.xaml(资源字典)中有以下样式代码如何将其链接到按钮?

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

    <Style x:Key="Button" TargetType="Button">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border>
                        <Rectangle Margin="2" Stroke="#60000000" StrokeThickness="1" StrokeDashArray="1 2"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

采纳答案by F????? A???

For the border glow effect, you can add a Drop Shadow Effectwith a Shadow Depthof 0 and the desired color and a border brush of white for the Button.

对于边界光晕效果,你可以添加阴影效果Shadow Depth0和所需的颜色和白色的按钮边框刷。

To create the style in a separate file, you need to create a Resource Dictionary like this:

要在单独的文件中创建样式,您需要像这样创建一个资源字典:

Project? Add New Item...? Resource Dictionary

项目添加新项目...资源字典

Then name your file. In this example, I name it Styles.xaml.

然后命名您的文件。在本例中,我将其命名为Styles.xaml

Now open up your App.xaml and put this within the Applicationtag:

现在打开你的 App.xaml 并将其放在Application标签中:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <!--Put all previous resources in the App.xaml here-->
    </ResourceDictionary>
</Application.Resources>

Now in the Styles.xaml file within the ResourceDictionarytag, put the style. I am working on a style too. Once I finish it I will post it.

现在在ResourceDictionary标签内的 Styles.xaml 文件中,放置样式。我也在研究一种风格。一旦我完成它,我会发布它。

To link the style, use:

要链接样式,请使用:

<Button Style="{StaticResource Button}" .../>

It may not work since the key is "Button". If it doesn't change it to something like "ButtonStyle".

它可能不起作用,因为键是“按钮”。如果它没有将其更改为“ButtonStyle”之类的内容。

And the Drop Shadow Effect does a pretty decent job.

投影效果做得相当不错。

Here's an example (Zoomed at 200%):

这是一个示例(放大 200%):

QWERTY

QWERTY

and the XAML style:

和 XAML 样式:

<Style x:Key="ButtonStyle" TargetType="Button">
    <Setter Property="BorderBrush">
        <Setter.Value>
            <SolidColorBrush Color="#FF00C3BA"/>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0.8" CornerRadius="3">
                    <Border.Effect>
                        <DropShadowEffect Color="#FF72FFE5" ShadowDepth="0"/>
                    </Border.Effect>
                    <TextBlock Foreground="{TemplateBinding BorderBrush}" Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

used like this:

像这样使用:

<Grid Background="#FF171717">
    <Button Content="Q" HorizontalAlignment="Left" Height="47" Margin="103,59,0,0" VerticalAlignment="Top" Width="50" FontSize="20" FontFamily="Segoe UI Light" Style="{StaticResource ButtonStyle}"/>
    <Button Content="Y" HorizontalAlignment="Left" Height="47" Margin="378,59,0,0" VerticalAlignment="Top" Width="50" FontSize="20" FontFamily="Segoe UI Light" Style="{StaticResource ButtonStyle}"/>
    <Button Content="T" HorizontalAlignment="Left" Height="47" Margin="323,59,0,0" VerticalAlignment="Top" Width="50" FontSize="20" FontFamily="Segoe UI Light" Style="{StaticResource ButtonStyle}"/>
    <Button Content="R" HorizontalAlignment="Left" Height="47" Margin="268,59,0,0" VerticalAlignment="Top" Width="50" FontSize="20" FontFamily="Segoe UI Light" Style="{StaticResource ButtonStyle}"/>
    <Button Content="E" HorizontalAlignment="Left" Height="47" Margin="213,59,0,0" VerticalAlignment="Top" Width="50" FontSize="20" FontFamily="Segoe UI Light" Style="{StaticResource ButtonStyle}"/>
    <Button Content="W" HorizontalAlignment="Left" Height="47" Margin="158,59,0,0" VerticalAlignment="Top" Width="50" FontSize="20" FontFamily="Segoe UI Light" Style="{StaticResource ButtonStyle}"/>
</Grid>