WPF Margin renders differently on windows XP / 7

2019-07-19 22:47发布

问题:

I have tried the hack located at http://arbel.net/2006/11/03/forcing-wpf-to-use-a-specific-windows-theme/ but it had no effect on this problem, there might be some ScaleTransform involved here but the value of it is the same in both pictures. Does any one know why there is such a big difference and is there any way to fix it?

Here is the style of the button:

<Style x:Key="activityButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Margin" Value="0,3,0,3" />
    <Setter Property="Padding" Value="5" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Background" Value="DarkGray" />
    <Setter Property="BorderThickness" Value="2" />
    <Setter Property="BorderBrush" Value="Black"/>
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0:0:0.2"/>
                            </VisualStateGroup.Transitions>
                            <VisualState x:Name="Normal">
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="pushedBorder">
                                        <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                    </DoubleAnimationUsingKeyFrames>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="pushedBorder2">
                                        <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                    </DoubleAnimationUsingKeyFrames>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="pushedBorder3">
                                        <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="pushedBorder">
                                        <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                    </DoubleAnimationUsingKeyFrames>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="pushedBorder2">
                                        <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                    </DoubleAnimationUsingKeyFrames>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="pushedBorder3">
                                        <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled"/>
                            <VisualState x:Name="MouseOver"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" />
                    <Border x:Name="normalBorder" BorderThickness="{TemplateBinding BorderThickness}">
                        <Border.Background>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#00000000" Offset="0.5"/>
                                <GradientStop Color="#19000000" Offset="0.51"/>
                            </LinearGradientBrush>
                        </Border.Background>
                    </Border>
                    <Border x:Name="pushedBorder4" BorderThickness="{TemplateBinding BorderThickness}">
                        <Border.Background>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="#00000000" Offset="0.5"/>
                                <GradientStop Color="#19000000" Offset="0.51"/>
                            </LinearGradientBrush>
                        </Border.Background>
                    </Border>
                    <Border x:Name="pushedBorder"  RenderTransformOrigin="0.5,0.5" Opacity="0" BorderBrush="#33000000" BorderThickness="3" CornerRadius="5"/>
                    <Border x:Name="pushedBorder2"  RenderTransformOrigin="0.5,0.5" Opacity="0" BorderBrush="#33000000" BorderThickness="4" CornerRadius="5"/>
                    <Border x:Name="pushedBorder3"  RenderTransformOrigin="0.5,0.5" Opacity="0" BorderBrush="#33000000" BorderThickness="5" CornerRadius="5"/>
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="5" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
标签: wpf .net-4.0