简体   繁体   English

WPF中Button的实现样式

[英]Implementing style for Button in WPF

I have a quick question. 我有一个快速的问题。 I have implemented style for button. 我已经实现了按钮样式。 And it basically works. 它基本上可以工作。 Here is the code (complete example: you can copy and paste, it will work): 这是代码(完整示例:您可以复制和粘贴,它将起作用):

<Window x:Class="TestWPFApplication.Window5"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestWPFApplication"
        mc:Ignorable="d"
        Title="Window5" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type Button}" x:Key="AButton">
            <Setter Property="Width" Value="120"></Setter>
            <Setter Property="Height" Value="50"></Setter>
            <Setter Property="Background" Value="DarkGreen" />
            <Setter Property="Foreground" Value="LightGreen" />
            <Setter Property="FontSize" Value="22" />
            <Setter Property="FontWeight" Value="Light" />
            <Setter Property="SnapsToDevicePixels" Value="True" />

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border CornerRadius="15" Background="{TemplateBinding Background}">
                            <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" 
                                              HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,0" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Button x:Name="QuitButton" Style="{StaticResource AButton}" Content="Quit" />
    </Grid>
</Window>

It's all fine. 没事的 Button looks like this: 按钮看起来像这样:

在此处输入图片说明

BUT If I move slightly the button, and the Margin property will be auto-generated, to this... 但是,如果我稍微移动按钮,则Margin属性将自动生成到此...

<Button x:Name="QuitButton" Style="{StaticResource AButton}" Content="Quit" Margin="88,114,87,108.5" />

.. button will look like this: ..按钮将如下所示:

在此处输入图片说明

Right and bottom side of the button has been cut off. 按钮的右侧和底部已被切除。 Don't know why:/ The question is: Can anyone explain me this? 不知道为什么:/问题是:有人可以向我解释一下吗? Thanks in advance! 提前致谢!

You've set the top margin to 114 and the bottom margin to 108.5 for a total of 225.5, but you've also set the total Window height to 300. That leaves just 77.5 pixels for the caption bar, the top and bottom window borders and the button (which you've set to 120 pixels high). 您已将顶部页边距设置为114,底部页边距设置为108.5,总共为225.5,但是您还将窗口总高度设置为300。这仅为标题栏,顶部和底部窗口边框留了77.5像素和按钮(已设置为120像素高)。 The only way for WPF to make everything fit is to crop the button. WPF使一切变得合适的唯一方法是裁剪按钮。 (The same thing is happening in the X axis). (在X轴上发生了相同的事情)。

Set WindowStyle="None" and ResizeMode="NoResize" on your main window and you'll see the button now has enough room to draw fine. 在主窗口上设置WindowStyle="None"ResizeMode="NoResize" ,您会看到该按钮现在有足够的空间进行精细绘制。 Better yet, set the right and bottom margin values to 0 and you can now set left and top to whatever you want. 更好的是,将右边距和底部边距值设置为0,现在可以将left和top设置为所需的值。

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

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