【问题标题】:Different height of Button with image in wp8wp8中带有图像的按钮高度不同
【发布时间】:2014-11-28 05:47:40
【问题描述】:

我在我的 xaml 中有一个按钮定义为

<Button BorderBrush="Transparent" Click="Menu_Btn" Margin="71,584,0,-2" Height="36" Width="38" Padding="0">
    <Image Source="Assets/menu.png" Stretch="UniformToFill" Height="36" Width="38"/>
</Button>

可以看出,我专门设置了按钮的高度和宽度等于图像,但是在 xaml 中它不能正确地包装图像并显示更多的宽度和高度。怎么回事?

为什么这个框更大,为什么我不能正确设置margin而不是40,我必须给71个值。

【问题讨论】:

    标签: c# html windows-phone-8


    【解决方案1】:

    &lt;Button&gt; 有一个额外的边距,它不是您可以在“属性”框中编辑的“属性”的一部分。但是,您可以通过编辑它的模板来编辑它。

    在线文档 -> 选择按钮 -> 编辑模板 -> 编辑副本

    去掉边框上的Margin="{StaticResource PhoneTouchTargetOverhang}",它会正确拉伸。


    <Style x:Key="ButtonStyle1" TargetType="Button">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
        <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneButtonBasePressedForegroundBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0">
                            <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    <Button BorderBrush="Transparent" Click="Menu_Btn" Margin="71,584,0,-2" Height="36" Width="38" Padding="0" Style="{StaticResource ButtonStyle1}">
        <Image Source="Assets/menu.png" Stretch="UniformToFill" Height="36" Width="38"/>
    </Button>
    

    【讨论】:

    • 你能告诉我在哪里可以获得所有键的列表,例如,如果我想知道 TextBox 等的键吗?
    • @MuhammadUmar 我不确定你的意思。您的意思是获取文本框中的内容吗?
    猜你喜欢
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多