【问题标题】:How to remove space between buttons in stackpanel C# xaml?如何删除stackpanel C# xaml中按钮之间的空间?
【发布时间】:2014-08-11 16:45:39
【问题描述】:

在 xaml 布局中,按钮之间没有空格。但是,当我在模拟器或设备中测试它时,按钮之间有空格。我怎样才能解决这个问题?

<phone:PhoneApplicationPage
    x:Class="KentKart.Pages.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

    <phone:PhoneApplicationPage.Resources>
        <Style x:Key="SimpleButton" TargetType="Button">
            <Setter Property="Margin" Value=",,,-13"/>
            <Setter Property="Padding" Value="40,5,0,5"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="BorderThickness" Value="2"/>
            <Setter Property="BorderBrush" Value="#0079c8"/>
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalAlignment" Value="Stretch"/>
            <Setter Property="Background" Value="White"/>
            <Setter Property="Foreground" Value="#0079c8"/>
        </Style>
    </phone:PhoneApplicationPage.Resources>

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid.Background>
            <ImageBrush Stretch="Fill" ImageSource="/Assets/Images.xcassets/Background.imageset/bg@2x.png" AlignmentY="Top" AlignmentX="Center"/>

        </Grid.Background>
        <Grid Background="#0079c8" Height="92" VerticalAlignment="Top">
            <TextBlock Width="auto" x:Name="selectedCityText" Foreground="White"  Text="" RenderTransformOrigin="-0.243,0.771" FontSize="15" Margin="0,10,0,28" />
        </Grid>
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Grid.Row="0" Margin="12,17,0,28">
            <Button Content="X" BorderThickness="0" HorizontalAlignment="Left" Margin="367,0,-12,0" VerticalAlignment="Top" Height="64" Width="113" Click="X_Button" RenderTransformOrigin="0.519,0.125"/>
        </StackPanel>
        <StackPanel VerticalAlignment="Top" Margin="0,92,0,0">

            <Button Style="{StaticResource SimpleButton}" Content="deneme"/>
            <Button Style="{StaticResource SimpleButton}" Content="deneme"/>
            <Button Style="{StaticResource SimpleButton}" Content="deneme"/>

        </StackPanel>


        <!--ContentPanel - place additional content here-->
    </Grid>



</phone:PhoneApplicationPage>

【问题讨论】:

  • 尝试更改margin 中的button
  • 我试过这个:
  • 试试&lt;Setter Property="Margin" Value=",-13,,-26"/&gt;
  • 不,没用,结果一样。
  • 你能显示你所看到的截图吗?如果您不想要空格,为什么不只是 Margin="0" (尽管通常不需要,因为它通常是默认值。)

标签: c# xaml button windows-phone


【解决方案1】:

稍作调整后,解决方案是

<Setter Property="Margin" Value="0,-10,0,-10"/>

您需要移除 20 像素的边距。但是左侧和右侧的0 至关重要,当您将它们留空时,您可以看到默认值为负(它们伸出框架)。在设计器和设备上的呈现方式似乎有所不同。

所以这里可能存在一个值得报告的潜在错误。

【讨论】:

  • 用负边距做这件事很奇怪,但它有效,谢谢。
  • 按钮的设计考虑到了胖手指(:
  • 啊,很周到。=)