相同的按钮,但看起来不同WPF

时间:2017-03-12 12:30:50

标签: c# wpf button styles

我的Window.xaml上有几个按钮, 有趣的是,当我运行应用程序时,即使我为每个应用程序使用相同的代码,它们也会有所不同。

这是截图:

enter image description here

可以注意到它们的边框看起来不一样(特别是在Y按钮上)..

以下是我的每个自定义按钮的代码:(X,Y和Z)

<Button x:Name="btnX"
        Grid.Column="1"
        FontSize="15"
        BorderThickness="1"
        Content="X"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center"
        Foreground="Black"
        Background="#d3d3d3"
        BorderBrush="Black" Margin="5">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border BorderThickness="{TemplateBinding BorderThickness}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                </Border>
            </ControlTemplate>
        </Button.Template>
    </Button>

    <Button x:Name="btnZ"
        Grid.Column="2"
        FontSize="15"
        Content="Z"
        BorderThickness="1"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center"
        Foreground="Black"
        Background="#d3d3d3"
        BorderBrush="Black" Margin="5">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border BorderThickness="{TemplateBinding BorderThickness}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                </Border>
            </ControlTemplate>
        </Button.Template>

    </Button>


        <Button x:Name="btnY"
        Grid.Column="2"
        Grid.Row="1"
        FontSize="15"
        Content="Y"
        BorderThickness="1"
        HorizontalContentAlignment="Center"
        VerticalContentAlignment="Center"
        Foreground="Black"
        Background="#d3d3d3"
        BorderBrush="Black" Margin="5">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border BorderThickness="{TemplateBinding BorderThickness}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                </Border>
            </ControlTemplate>
        </Button.Template>
</Button>

1 个答案:

答案 0 :(得分:1)

我发现在我的按钮上设置SnapsToDevicePixels="True"UseLayoutRounding="True"修复了这个问题,我不确定这是否是最佳答案,但这肯定帮助了我..

设置此属性后的按钮:

enter image description here

Borders现在看起来一样,并且应该是黑色。