如何从网格的第二行开始换行文本块?
How to start wrapping of a textblock from second row of a Grid?
我在一个网格中有 2 行。第一行有 5 列,每一列都有一个文本块。在第二行我有一个分隔线。
我可以包装最后一个文本块,以便它应该从网格的第二行开始吗?
目前这是我的 xaml 代码。
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding FullName}" Grid.Row="0" Grid.Column="0" x:Name="firstName" Visibility="{Binding FullName, Converter={StaticResource NullOrWhiteSpaceConverter}}" TextAlignment="Left"/>
<TextBlock Text=" | " FontWeight="Bold" Foreground="#008DFC" Grid.Column="1" Grid.Row="0" Margin="0,0,0,0" Visibility="{Binding Visibility,ElementName=firstlevel}" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding FirstLevelParent}" Grid.Row="0" Margin="10,0,0,0" Visibility="{Binding FirstLevelParent, Converter={StaticResource NullOrWhiteSpaceConverter}}" x:Name="firstlevel" Grid.Column="1" TextAlignment="Left"/>
<TextBlock Text=" | " Foreground="#008DFC" FontWeight="Bold" Grid.Column="1" Grid.Row="0" Visibility="{Binding Visibility,ElementName=middleName}" Margin="0,0,-10,0" HorizontalAlignment="Right"/>
<TextBlock Text="{Binding SecondLevelParent}" Grid.Row="0" Margin="10,0,0,0" Visibility="{Binding SecondLevelParent, Converter={StaticResource NullOrWhiteSpaceConverter}}" x:Name="middleName" Grid.Column="2" TextAlignment="Left"/>
<TextBlock Text=" | " Foreground="#008DFC" FontWeight="Bold" Grid.Column="2" Grid.Row="0" Visibility="{Binding Visibility,ElementName=lastName}" Margin="0,0,-10,0" HorizontalAlignment="Right"/>
<TextBlock Text="{Binding ThreeLevelParent}" Grid.Row="0" Margin="10,0,0,0" x:Name="lastName" Visibility="{Binding ThreeLevelParent, Converter={StaticResource NullOrWhiteSpaceConverter}}" Grid.Column="3" TextAlignment="Left"/>
<TextBlock Text=" | " Foreground="#008DFC" FontWeight="Bold" Grid.Column="3" Grid.Row="0" Visibility="{Binding Visibility,ElementName=fourlevel}" Margin="0,0,-10,0" HorizontalAlignment="Right"/>
<TextBlock Text="{Binding FourLevelParent}" TextWrapping="Wrap" Grid.Row="0" Margin="10,0,0,0" x:Name="fourlevel" Visibility="{Binding FourLevelParent, Converter={StaticResource NullOrWhiteSpaceConverter}}" Grid.Column="4" TextAlignment="Left"/>
<Grid Grid.Row="1" x:Name="deliverypoint" Visibility="Collapsed">
<!--<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>-->
<TextBlock Text="{Binding DeliveryPoint}" x:Name="deliverypointname" TextAlignment="Left" Foreground="Black" FontSize="12"/>
</Grid>
<!--<TextBlock Text="|" Grid.Column="2" Margin="0,0,-6,0" Visibility="{Binding Visibility,ElementName=lastName}" HorizontalAlignment="Right"/>-->
</Grid>
<TextBlock TextWrapping="NoWrap" Text="-------------------------------------------------------------------------" Grid.Row="1"/>
</Grid>
提前致谢。
我建议您不要使用 'Grid.ColumnDefinition' 来布局您的 TextBlock。相反,您可以使用 WrapPanel XAML Control 来包装这些 TextBlock。达到最大宽度或高度后,控件会根据方向自动创建行或列。
我做了一个简单的代码示例供大家参考:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<controls:WrapPanel Orientation="Horizontal" HorizontalSpacing="10">
<TextBlock Text="FullName" x:Name="firstName" TextAlignment="Left"/>
<TextBlock Text=" | " FontWeight="Bold" Foreground="#008DFC" HorizontalAlignment="Left"/>
<TextBlock Text="FirstLevelParent" x:Name="firstlevel" TextAlignment="Left"/>
<TextBlock Text=" | " Foreground="#008DFC" FontWeight="Bold" HorizontalAlignment="Right"/>
<TextBlock Text="SecondLevelParent" x:Name="middleName" TextAlignment="Left"/>
<TextBlock Text=" | " Foreground="#008DFC" FontWeight="Bold" HorizontalAlignment="Right"/>
<TextBlock Text="ThreeLevelParent" x:Name="lastName" TextAlignment="Left"/>
<TextBlock Text=" | " Foreground="#008DFC" FontWeight="Bold" HorizontalAlignment="Right"/>
<TextBlock Text="FourLevelParentFourLevelParentFourLevelParentFourLevelParentFourLevelParentFourLevelParentFourLevelParent" x:Name="fourlevel" TextAlignment="Left" TextWrapping="Wrap"/>
</controls:WrapPanel>
<TextBlock TextWrapping="NoWrap" Text="---------------------------------------------------------------------------------------" VerticalAlignment="Top" Grid.Row="1"/>
</Grid>
我在一个网格中有 2 行。第一行有 5 列,每一列都有一个文本块。在第二行我有一个分隔线。 我可以包装最后一个文本块,以便它应该从网格的第二行开始吗? 目前这是我的 xaml 代码。
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding FullName}" Grid.Row="0" Grid.Column="0" x:Name="firstName" Visibility="{Binding FullName, Converter={StaticResource NullOrWhiteSpaceConverter}}" TextAlignment="Left"/>
<TextBlock Text=" | " FontWeight="Bold" Foreground="#008DFC" Grid.Column="1" Grid.Row="0" Margin="0,0,0,0" Visibility="{Binding Visibility,ElementName=firstlevel}" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding FirstLevelParent}" Grid.Row="0" Margin="10,0,0,0" Visibility="{Binding FirstLevelParent, Converter={StaticResource NullOrWhiteSpaceConverter}}" x:Name="firstlevel" Grid.Column="1" TextAlignment="Left"/>
<TextBlock Text=" | " Foreground="#008DFC" FontWeight="Bold" Grid.Column="1" Grid.Row="0" Visibility="{Binding Visibility,ElementName=middleName}" Margin="0,0,-10,0" HorizontalAlignment="Right"/>
<TextBlock Text="{Binding SecondLevelParent}" Grid.Row="0" Margin="10,0,0,0" Visibility="{Binding SecondLevelParent, Converter={StaticResource NullOrWhiteSpaceConverter}}" x:Name="middleName" Grid.Column="2" TextAlignment="Left"/>
<TextBlock Text=" | " Foreground="#008DFC" FontWeight="Bold" Grid.Column="2" Grid.Row="0" Visibility="{Binding Visibility,ElementName=lastName}" Margin="0,0,-10,0" HorizontalAlignment="Right"/>
<TextBlock Text="{Binding ThreeLevelParent}" Grid.Row="0" Margin="10,0,0,0" x:Name="lastName" Visibility="{Binding ThreeLevelParent, Converter={StaticResource NullOrWhiteSpaceConverter}}" Grid.Column="3" TextAlignment="Left"/>
<TextBlock Text=" | " Foreground="#008DFC" FontWeight="Bold" Grid.Column="3" Grid.Row="0" Visibility="{Binding Visibility,ElementName=fourlevel}" Margin="0,0,-10,0" HorizontalAlignment="Right"/>
<TextBlock Text="{Binding FourLevelParent}" TextWrapping="Wrap" Grid.Row="0" Margin="10,0,0,0" x:Name="fourlevel" Visibility="{Binding FourLevelParent, Converter={StaticResource NullOrWhiteSpaceConverter}}" Grid.Column="4" TextAlignment="Left"/>
<Grid Grid.Row="1" x:Name="deliverypoint" Visibility="Collapsed">
<!--<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>-->
<TextBlock Text="{Binding DeliveryPoint}" x:Name="deliverypointname" TextAlignment="Left" Foreground="Black" FontSize="12"/>
</Grid>
<!--<TextBlock Text="|" Grid.Column="2" Margin="0,0,-6,0" Visibility="{Binding Visibility,ElementName=lastName}" HorizontalAlignment="Right"/>-->
</Grid>
<TextBlock TextWrapping="NoWrap" Text="-------------------------------------------------------------------------" Grid.Row="1"/>
</Grid>
提前致谢。
我建议您不要使用 'Grid.ColumnDefinition' 来布局您的 TextBlock。相反,您可以使用 WrapPanel XAML Control 来包装这些 TextBlock。达到最大宽度或高度后,控件会根据方向自动创建行或列。
我做了一个简单的代码示例供大家参考:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<controls:WrapPanel Orientation="Horizontal" HorizontalSpacing="10">
<TextBlock Text="FullName" x:Name="firstName" TextAlignment="Left"/>
<TextBlock Text=" | " FontWeight="Bold" Foreground="#008DFC" HorizontalAlignment="Left"/>
<TextBlock Text="FirstLevelParent" x:Name="firstlevel" TextAlignment="Left"/>
<TextBlock Text=" | " Foreground="#008DFC" FontWeight="Bold" HorizontalAlignment="Right"/>
<TextBlock Text="SecondLevelParent" x:Name="middleName" TextAlignment="Left"/>
<TextBlock Text=" | " Foreground="#008DFC" FontWeight="Bold" HorizontalAlignment="Right"/>
<TextBlock Text="ThreeLevelParent" x:Name="lastName" TextAlignment="Left"/>
<TextBlock Text=" | " Foreground="#008DFC" FontWeight="Bold" HorizontalAlignment="Right"/>
<TextBlock Text="FourLevelParentFourLevelParentFourLevelParentFourLevelParentFourLevelParentFourLevelParentFourLevelParent" x:Name="fourlevel" TextAlignment="Left" TextWrapping="Wrap"/>
</controls:WrapPanel>
<TextBlock TextWrapping="NoWrap" Text="---------------------------------------------------------------------------------------" VerticalAlignment="Top" Grid.Row="1"/>
</Grid>