为什么保证金不适用于WPF?

时间:2011-10-15 11:20:47

标签: wpf xaml styles

<StackPanel Grid.Column="1" Orientation="Horizontal" Margin="0,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Right">
    <StackPanel.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Margin" Value="2,0,0,0"/>
        </Style>
    </StackPanel.Resources> 
    <Button Padding="5" x:Name="btnUnlock" Click="btnUserMenu_Click_1" Style="{DynamicResource ButtonStyleBasic}">
            <Image Stretch="None" Source="/WpfApplication1;component/Images/view_text.png" ImageFailed="Image_ImageFailed"/>
    </Button>
    <Button Padding="5"  Margin="2,0,0,0" x:Name="btnUserMenu" Click="btnUserMenu_Click_1" Style="{DynamicResource ButtonStyleBasic}"  >
        <Image Stretch="None" Source="/WpfApplication1;component/Images/personal.png" ImageFailed="Image_ImageFailed"/>
    </Button>
    <Button Padding="5" x:Name="btnQuit"   Margin="2,0,0,0" Click="btnQuit_Click" Style="{DynamicResource ButtonStyleBasic}">
            <Image Stretch="None" Source="/WpfApplication1;component/Images/exit.png" UseLayoutRounding="True"/>
    </Button>
    </StackPanel>

如果我删除<Button ...内的边距,则不会应用边距。当我应用自定义模板时就是这种情况..这是模板......

<Style x:Key="ButtonStyleBasic" TargetType="{x:Type Button}">

            <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
            <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
            <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
            <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="1"/>

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true" CornerRadius="2" BorderThickness="{TemplateBinding BorderThickness}">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsKeyboardFocused" Value="true">

                            </Trigger>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">

                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="#ADADAD"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

1 个答案:

答案 0 :(得分:1)

您的样式中的模板未使用Margin绑定到TemplateBinding,因此Buttons上具有此样式的边距属性将被忽略。