【问题标题】:How to prevent textblock to overlapping?如何防止文本块重叠?
【发布时间】:2015-12-06 18:20:40
【问题描述】:

我已经尝试过ViewBox 来解决这个问题,但是对于某些分辨率,结果很奇怪,字体太大,我想保持我的字体大小,所以ViewBox 不适合这个目标。这是我的结构:

<StackPanel Orientation="Vertical"  Grid.Column="0" Grid.Row="5">
          <TextBlock TextDecorations="Underline" FontWeight="Bold" HorizontalAlignment="Center">Fattore forma (casa)</TextBlock>
          <Label Content="40%" HorizontalAlignment="Center" FontWeight="Bold" FontSize="20"></Label>
</StackPanel>

<StackPanel Orientation="Vertical" Grid.Column="1" Grid.Row="5" Margin="5,0">
           <TextBlock TextDecorations="Underline" FontWeight="Bold" HorizontalAlignment="Center">Fattore forma (fuori)</TextBlock>
           <Label Content="35%" HorizontalAlignment="Center" FontWeight="Bold" FontSize="20"></Label>
</StackPanel>

<StackPanel Orientation="Vertical" Grid.Column="2" Grid.Row="5">
           <TextBlock TextDecorations="Underline" FontWeight="Bold" HorizontalAlignment="Center">Totali giocate</TextBlock>
           <Label Content="9" HorizontalAlignment="Center" FontWeight="Bold" FontSize="20"></Label>
</StackPanel>

现在的主要问题是,如果我将窗口大小调整到最小分辨率,我会得到一个文本重叠并且我想避免这种情况,这样做的最佳主意是什么?我是 wpf 的新手,所以我实际上正在学习解决这个问题的最佳解决方案。

图片示例:

建议的新代码:

<Grid Grid.Column="0" Grid.Row="5" Grid.ColumnSpan="3">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="15" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <TextBlock TextDecorations="Underline" FontWeight="Bold" HorizontalAlignment="Center" Grid.Column="0" Grid.Row="0">Fattore forma (casa)</TextBlock>
                                <Label Content="40%" HorizontalAlignment="Center" FontWeight="Bold" FontSize="20" Grid.Column="0" Grid.Row="1"></Label>
                                <TextBlock TextDecorations="Underline" FontWeight="Bold" HorizontalAlignment="Center" Grid.Column="1" Grid.Row="0">Fattore forma (fuori)</TextBlock>
                                <Label Content="35%" HorizontalAlignment="Center" FontWeight="Bold" FontSize="20" Grid.Column="1" Grid.Row="1"></Label>
                                <TextBlock TextDecorations="Underline" FontWeight="Bold" HorizontalAlignment="Center" Grid.Column="2" Grid.Row="0">Totali giocate</TextBlock>
                                <Label Content="9" HorizontalAlignment="Center" FontWeight="Bold" FontSize="20" Grid.Column="2" Grid.Row="1"></Label>
                            </Grid>

【问题讨论】:

  • 使用 Grid 或 Uniform Grid 代替 StackPanel 并定义列和行定义。
  • 在我看来,您应该提供您管理的结果的屏幕截图。
  • 会很高兴看到屏幕截图..也许第二个有理想的结果。
  • 按照建议,我已经用 Grid 和 image 编辑了我的答案,但问题仍然与最小分辨率相同。

标签: c# wpf xaml


【解决方案1】:

似乎StackPanels 在带有行和列定义的Grid 中,而您没有粘贴整个代码。

但是,您可以在TextBlock 中使用TextTrimming="CharacterEllipsis"。文字过长时会自动加点。

或者TextWrapping="Wrap",如果你想将文本换行。

【讨论】:

  • 似乎是完美的选择
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-05
相关资源
最近更新 更多