如何最好地使用动态文本调整 XAML 大小

发布于 2024-07-26 20:20:48 字数 2584 浏览 2 评论 0原文

在我正在开发的 Silverlight 界面中,我需要在容器调整大小时调整堆栈面板的大小,以防止其中的文本被截断。 相关文本是在运行时从语言文件添加的,具体取决于用户选择的语言。 由于不同语言之间字符串的长度可能有很大差异,我对如何最好地解决这个问题感到有点困惑。 这就是它的样子。

alt text

有问题的堆栈面板是彩色块和标签“驾驶”、“积极工作”、“可用性”和“休息”是长度可以改变的字符串。 XAML 当前看起来像这样。

<StackPanel x:Name="StatusTotals" Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,5,0,5">
  <Border x:Name="DrivingBorder" Background="#80FF0000" BorderBrush="Black" BorderThickness="1" Opacity="1" CornerRadius="5,0,0,5" Margin="0,0,-1,0">
    <StackPanel Orientation="Vertical" Margin="5,1" VerticalAlignment="Center">
      <TextBlock Text="Driving" Tag="dttlDriving" HorizontalAlignment="Center"/>
      <TextBlock x:Name="DrivingDuration" Text="00:00" HorizontalAlignment="Center"/>
    </StackPanel>
  </Border>
  <Border x:Name="ActiveWorkBorder" Background="#8000FFFF" BorderBrush="Black" BorderThickness="1" Opacity="1" Margin="0,0,-1,0">
    <StackPanel Orientation="Vertical" Margin="5,1" VerticalAlignment="Center">
      <TextBlock Text="ActiveWork" Tag="dttlActiveWork" HorizontalAlignment="Center"/>
      <TextBlock x:Name="ActiveWorkDuration" Text="00:00" HorizontalAlignment="Center"/>
    </StackPanel>
  </Border>
  <Border x:Name="AvailabilityBorder" Background="#80FFFF00" BorderBrush="Black" BorderThickness="1" Opacity="1" Margin="0,0,-1,0">
    <StackPanel Orientation="Vertical" Margin="5,1" VerticalAlignment="Center">
      <TextBlock Text="Availability" Tag="dttlAvailability" HorizontalAlignment="Center"/>
      <TextBlock x:Name="AvailabilityDuration" Text="00:00" HorizontalAlignment="Center"/>
    </StackPanel>
  </Border>
  <Border x:Name="BreakBorder" Background="#8000FF00" BorderBrush="Black" BorderThickness="1" Opacity="1" CornerRadius="0,5,5,0">
    <StackPanel Orientation="Vertical" Margin="5,1" VerticalAlignment="Center">
      <TextBlock Text="Break" Tag="dttlBreak" HorizontalAlignment="Center"/>
      <TextBlock x:Name="BreakDuration" Text="00:00" HorizontalAlignment="Center"/>
    </StackPanel>
  </Border>
</StackPanel>

我希望能够减小该面板的大小(如果它比上面绘制的标尺更宽),该标尺已经根据我的需要动态调整大小,但希望标签下方的值保持其原来的大小的可读性。

解决这个问题的最佳方法是什么? 我考虑过设置标签的 FontSize 属性或使用 ScaleTransform,但我不确定其中哪一个是最好的方法。 有没有一种方法可以导出面板将采用的宽度,而无需实际更改字体大小,然后测试 ActualWidth? 如果我能做到这一点,这项任务就会变得更容易。

In a Silverlight interface I'm working on I have the need to resize a stack panel when its container resizes, to prevent the text in it from being truncated. The text in question is added at runtime from language files, depending on the language the user has selected. As the lengths of strings can vary considerably between languages I'm a bit stumped on how best to approach this. This is what it looks like.

alt text

The stack panel in question is the coloured block and the labels Driving, Active Work, Availability and Break are the strings whose length can change. The XAML currently looks like this.

<StackPanel x:Name="StatusTotals" Grid.Row="1" Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,5,0,5">
  <Border x:Name="DrivingBorder" Background="#80FF0000" BorderBrush="Black" BorderThickness="1" Opacity="1" CornerRadius="5,0,0,5" Margin="0,0,-1,0">
    <StackPanel Orientation="Vertical" Margin="5,1" VerticalAlignment="Center">
      <TextBlock Text="Driving" Tag="dttlDriving" HorizontalAlignment="Center"/>
      <TextBlock x:Name="DrivingDuration" Text="00:00" HorizontalAlignment="Center"/>
    </StackPanel>
  </Border>
  <Border x:Name="ActiveWorkBorder" Background="#8000FFFF" BorderBrush="Black" BorderThickness="1" Opacity="1" Margin="0,0,-1,0">
    <StackPanel Orientation="Vertical" Margin="5,1" VerticalAlignment="Center">
      <TextBlock Text="ActiveWork" Tag="dttlActiveWork" HorizontalAlignment="Center"/>
      <TextBlock x:Name="ActiveWorkDuration" Text="00:00" HorizontalAlignment="Center"/>
    </StackPanel>
  </Border>
  <Border x:Name="AvailabilityBorder" Background="#80FFFF00" BorderBrush="Black" BorderThickness="1" Opacity="1" Margin="0,0,-1,0">
    <StackPanel Orientation="Vertical" Margin="5,1" VerticalAlignment="Center">
      <TextBlock Text="Availability" Tag="dttlAvailability" HorizontalAlignment="Center"/>
      <TextBlock x:Name="AvailabilityDuration" Text="00:00" HorizontalAlignment="Center"/>
    </StackPanel>
  </Border>
  <Border x:Name="BreakBorder" Background="#8000FF00" BorderBrush="Black" BorderThickness="1" Opacity="1" CornerRadius="0,5,5,0">
    <StackPanel Orientation="Vertical" Margin="5,1" VerticalAlignment="Center">
      <TextBlock Text="Break" Tag="dttlBreak" HorizontalAlignment="Center"/>
      <TextBlock x:Name="BreakDuration" Text="00:00" HorizontalAlignment="Center"/>
    </StackPanel>
  </Border>
</StackPanel>

I would like to be able to reduce the size of this panel if it's wider than the ruler drawn above it, which is already dynamically resizing as I want, but would like the values beneath the labels to stay at the size they are for the sake of readability.

What would be the best way to approach this? I've considered setting the FontSize properties of the labels or using a ScaleTransform but I'm unsure which of these is the best way to go. Is there a way to derive the width that the panel will adopt without actually changing the font sizes and then testing ActualWidth? If I could do this it would make this task easier.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北风几吹夏 2024-08-02 20:20:48

我不确定我是否完全理解你的问题。

您的顶级父堆栈面板是否已被赋予 x 宽的限制,但由于标签的长度,它的子级要求总宽度大于此宽度?

在这种情况下,请查看将标签包装在 ViewBox 控件中(您可以在 Silverlight 工具箱库) 该控件将缩放其内容以占据可用的大小。

I'm not sure if I understand your question completely.

Is it a case of your top parent stackpanel has been given a limit of x wide, but it's children are asking for a total width that is greater than this due to length of the labels?

In which case have a look at wrapping the labels in ViewBox controls (You'll find it in the Silverlight Toolbox library) This control will scale it's contents to take up the size available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文