WPF内容Presenter奇怪的水平对齐行为

时间:2018-06-11 19:23:17

标签: c# wpf xaml

我试图将Content Presenter的内容集中在一个按钮中,它完美地集中在designer但是当它运行时,它会转移off center一点或者得到{{} 3}}。令我感到困惑的是,我有另一种风格做得相对完全相同,但效果很好。

这是风格;

<Style x:Key="MainButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="0,1"/>
            <Setter Property="RenderOptions.BitmapScalingMode" Value="NearestNeighbor"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid x:Name="Chrome" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                            <Grid.BindingGroup>
                                <BindingGroup/>
                            </Grid.BindingGroup>
                            <Border x:Name="border" CornerRadius="3,3,3,3" BorderBrush="LightGray" BorderThickness="1" Background="#FFF3F3F3" Padding="0">
                                <ContentPresenter 
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Stretch"
                                    TextElement.FontWeight="Light" Height="Auto"  Margin="153.636,0.52,156,1.52" Width="Auto" UseLayoutRounding="False" SnapsToDevicePixels="False" >
                                </ContentPresenter>

                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Background" TargetName="border" Value="#330CB3EE"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="BorderBrush" TargetName="border" Value="#FF0CB3EE"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Opacity" TargetName="Chrome" Value="0.25"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>

            </Setter>
        </Style>

1 个答案:

答案 0 :(得分:1)

从内容演示者中删除保证金:

<ContentPresenter HorizontalAlignment="Center"
                  VerticalAlignment="Stretch"
                  TextElement.FontWeight="Light"
                  Height="Auto"

                  Width="Auto" 
                  UseLayoutRounding="False"
                  SnapsToDevicePixels="False" >

这个保证金可能是由设计师添加的,与设计师中控件的布局相匹配。