重叠TextBlocks:为什么?

时间:2015-12-31 06:00:11

标签: c# xaml windows-phone-8

此代码应该生成一个4 x 3网格的笔记。一旦生成,TextBlocks重叠。 填充是尝试调整:失败。

代码:

PivotPage.xaml

<Page>
    <Grid>
        <Pivot x:Uid="Pivot" Title="MY APPLICATION" x:Name="pivot" CommonNavigationTransitionInfo.IsStaggerElement="True">
            <!--Pivot item one-->
            <PivotItem
                x:Uid="PivotItem1"
                Margin="19,14.5,0,0"
                CommonNavigationTransitionInfo.IsStaggerElement="True">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition Height="1*"/>
                    </Grid.RowDefinitions>
                    <ItemsControl ItemsSource="{Binding strings}">
                        <!--
                    -->
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapGrid MaximumRowsOrColumns="4" Orientation="Horizontal" />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding}" FontSize="64" Padding="27,0"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                    <Button Grid.Row="1" Content="Box It!"/>
                </Grid>
            </PivotItem>
        </Pivot>
    </Grid>
</Page>

更新:图片

This is usually worse to the point where they're nearly on top of each other.

2 个答案:

答案 0 :(得分:0)

我想出了我的问题,但有一点需要注意。

首先回答:

<强> PivotPage.xaml

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding}" FontSize="64" Margin="24,20"/>
    </DataTemplate>
</ItemsControl.ItemTemplate>

将其从“填充”更改为“边距”,稍微调整一下大小,并将其修复。

此外:

<Grid.RowDefinitions>
    <RowDefinition/>
    <RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

使高度“自动”,因此按钮的行不会继续移动。

需要注意的是,每次运行程序时,Grid的大小都会有所不同。出于这个原因,我建议(对自己)做更多这样的事情:

                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Row="0" Grid.Column="0" 
                                   Text="{Binding Notes[0],FallbackValue=B♭}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"
                                   FontSize="64"/>
                        <TextBlock Grid.Row="0" Grid.Column="1" 
                                   Text="{Binding Notes[1],FallbackValue=B♭}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"
                                   FontSize="64"/>
                        <TextBlock Grid.Row="0" Grid.Column="2" 
                                   Text="{Binding Notes[2],FallbackValue=B♭}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"
                                   FontSize="64"/>
                        <TextBlock Grid.Row="1" Grid.Column="0" 
                                   Text="{Binding Notes[3],FallbackValue=B♭}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"
                                   FontSize="64"/>
                        <TextBlock Grid.Row="1" Grid.Column="1" 
                                   Text="{Binding Notes[4],FallbackValue=B♭}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"
                                   FontSize="64"/>
                        <TextBlock Grid.Row="1" Grid.Column="2" 
                                   Text="{Binding Notes[5],FallbackValue=B♭}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"
                                   FontSize="64"/>
                        <TextBlock Grid.Row="2" Grid.Column="0" 
                                   Text="{Binding Notes[6],FallbackValue=B♭}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"
                                   FontSize="64"/>
                        <TextBlock Grid.Row="2" Grid.Column="1" 
                                   Text="{Binding Notes[7],FallbackValue=B♭}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"
                                   FontSize="64"/>
                        <TextBlock Grid.Row="2" Grid.Column="2" 
                                   Text="{Binding Notes[8],FallbackValue=B♭}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"
                                   FontSize="64"/>
                        <TextBlock Grid.Row="3" Grid.Column="0" 
                                   Text="{Binding Notes[9],FallbackValue=B♭}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"
                                   FontSize="64"/>
                        <TextBlock Grid.Row="3" Grid.Column="1" 
                                   Text="{Binding Notes[10],FallbackValue=B♭}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"
                                   FontSize="64"/>
                        <TextBlock Grid.Row="3" Grid.Column="2" 
                                   Text="{Binding Notes[11],FallbackValue=B♭}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"
                                   FontSize="64"/>
                    </Grid>

可能不赞成,但只有十二个音符,你不会做这么多,并且它将保证每次运行应用程序时网格保持相同的大小。

我仍然在寻找更好的解决方案,所以如果你有一个,请随时说出来。这是一个临时的答案,直到我能得到更好的东西。

顺便说一句:为了清楚起见,将“字符串”替换为“注释”。

答案 1 :(得分:0)

使用边距(它们为相关元素提供外部间距),而不是使用填充(为相关元素提供内部间距)。

将立即解决您的问题