网格 - 列之间没有空格

时间:2012-11-01 00:03:48

标签: silverlight windows-phone-7 xaml grid

我为ListBox定义了一些UserControl

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <ListBox Height="Auto" HorizontalAlignment="Stretch" Name="deliveriesListBox" VerticalAlignment="Stretch" Width="Auto" DataContext="{Binding}" SelectionChanged="deliveriesListBox_SelectionChanged">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid x:Name="LayoutRoot" xmlns:src="clr-namespace:App"  Margin="0,5">
                            <src:DItem />
                        </Grid>
                    </DataTemplate>    
                </ListBox.ItemTemplate>                    
            </ListBox>
        </Grid>

UserControl是

<UserControl x:Class="App.DItem"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480">

    <Grid x:Name="LayoutRoot" Margin="0,5">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="0" FontSize="42" HorizontalAlignment="Left"  Text="Hello" VerticalAlignment="Stretch" Margin="0" />
        <TextBlock Grid.Row="1" Grid.Column="1" FontSize="20" HorizontalAlignment="Right" TextAlignment="Right" Margin="0">
                                        <Run Text="OK" />

        </TextBlock>
        <TextBlock Grid.Row="1" Grid.Column="0" FontSize="20" Text="this is working only here.." HorizontalAlignment="Left"  />
    </Grid>
</UserControl>
VS2010或Expression Blend 4中的“WYSIWYG”编辑器显示“这只在这里工作”和“确定”之间的空格。想象一下 - http://xrep.eu/pic.jpg

这就是我想要的但是..在模拟器中,手机看起来像http://xrep.eu/emu.jpg

所以,在“这只在这里工作”和“确定”之间,没有空间。 有人可以帮忙告诉我如何定义,“OK”字符串最多只能在右侧吗?

2 个答案:

答案 0 :(得分:0)

尝试这个

 <Grid x:Name="LayoutRoot" Grid.Row="1" Margin="0,5">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0" FontSize="42" HorizontalAlignment="Left"  Text="Hello" VerticalAlignment="Stretch" Margin="0" />

        <DockPanel Grid.Row="1">
        <TextBlock  FontSize="20" HorizontalAlignment="Right" TextAlignment="Right" Margin="0" DockPanel.Dock="Right">
                                    <Run Text="OK" />

        </TextBlock>
        <TextBlock Grid.Row="1" Grid.Column="0" FontSize="20" Text="this is working only here.." HorizontalAlignment="Left"  />
        </DockPanel>
    </Grid>

干杯

答案 1 :(得分:0)

问题是父控件的宽度。

在设计模式下,正在执行以下代码(在UserControl的定义中):

mc:Ignorable="d"
d:DesignHeight="480" d:DesignWidth="480"

指定高度,更重要的是指定控件的宽度。这足够宽,所以你可以看到两段文字之间的空间。

在运行时,这将被忽略,以便Grid(和UserControl)获取正在显示的项目的宽度。

您有两种选择。

  1. 使用MarginPadding属性添加一些明确的间距。
  2. HorizontalAlignment的{​​{1}}设置为Grid
  3. 您可能需要做一些实验和调整才能获得您想要的效果。