【问题标题】:Dynamically reducing FontSize to avoid overflow动态减小 FontSize 以避免溢出
【发布时间】:2012-04-28 20:37:39
【问题描述】:

我用ButtonItemsControl 编写了一个玩具WPF 应用程序。每次单击Button 时,都会将字符串“AnotherWord”添加到ItemsControl。现在,ItemsControl 显示为水平方向的StackPanel,宽度固定(500 像素)。这意味着当您单击按钮一定次数(实际上是六次)时,新添加的字符串会被剪切,如下所示:

“另一个字另一个字另一个字另一个字另一个字另一个我”

FontSize 为 13 时会发生这种情况;如果你把它降低到 12.7,那么“AnotherWord”就有第六次出现的空间。我的问题是:有没有办法在运行时进行这种调整以避免溢出?

编辑:

在问题的上下文中,StackPanel 的固定宽度是强制性的 - 我们不能使用超过 500 个像素。另一个要求是字体不能大于 13。

这是我写的所有代码:

<!-- MainWindow.xaml -->
<Window x:Class="FontSize.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
         DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Window.Resources>
        <DataTemplate x:Key="labelTemplate">
            <Label FontSize="13" Content="AnotherWord"></Label>
        </DataTemplate>
        <ItemsPanelTemplate x:Key="panelTemplate">
            <StackPanel Orientation="Horizontal" Width="500" Height="50" />
        </ItemsPanelTemplate>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
            <ItemsControl Grid.Row="0" ItemsSource="{Binding Path=MyStrings}" ItemTemplate="{StaticResource labelTemplate}" 
                ItemsPanel="{StaticResource panelTemplate}" />
        <Button Grid.Row="1"  Click="Button_Click"></Button>
    </Grid>
</Window>

// MainWindow.xaml.cs
using System.Collections.ObjectModel;
using System.Windows;

namespace FontSize
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
            MyStrings = new ObservableCollection<string>();
        }

        public ObservableCollection<string> MyStrings
        {
            get { return (ObservableCollection<string>) GetValue(MyStringsProperty); }
            set { SetValue(MyStringsProperty, value); }
        }

        private static readonly DependencyProperty MyStringsProperty =
            DependencyProperty.Register("MyStrings", typeof (ObservableCollection<string>), typeof (Window));

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MyStrings.Add("AnotherWord");
        }
    }
}

【问题讨论】:

  • @paul 您的链接仅讨论如何计算大小。虽然这是需要完成的一件事,但它不是这个问题的答案。我想知道是否有人有一个实用程序可以做到这一点。
  • WPF 布局引擎可以通过扩展和收缩视觉元素来进行流畅的布局。但是,调整文本元素的字体大小不是该机制的一部分。你必须想出自己的算法。

标签: c# wpf overflow itemscontrol font-size


【解决方案1】:

将您的ItemsControl 放入Viewbox 并使用以下属性:

  • 最大宽度
  • 最大高度
  • 拉伸
  • 拉伸方向

编辑

并删除您的StackPanelWidthHeight 属性。

编辑 2

试试这样的:

<!-- MainWindow.xaml -->
<Window x:Class="FontSize.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
         DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Window.Resources>
        <DataTemplate x:Key="labelTemplate">
            <Label FontSize="13" Content="AnotherWord"></Label>
        </DataTemplate>
        <ItemsPanelTemplate x:Key="panelTemplate">
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Viewbox Grid.Row="0" MaxWidth="500" Stretch="Uniform">
            <ItemsControl 
                ItemsSource="{Binding Path=MyStrings}"
                ItemTemplate="{StaticResource labelTemplate}" 
                ItemsPanel="{StaticResource panelTemplate}" />
        </Viewbox>
        <Button Grid.Row="1"  Click="Button_Click"></Button>
    </Grid>
</Window>

编辑 3

更改Viewbox 的水平对齐方式,使其不会被拉伸以填充网格。我放了“中心”,用你想要的替换。

...
<Viewbox 
    HorizontalAlignment="Center" 
    StretchDirection="DownOnly"
    Grid.Row="0"
    MaxWidth="500"
    Stretch="Uniform">
...

【讨论】:

  • 好吧,在这个问题的上下文中,我们不能使用超过 500 个像素。抱歉没有说清楚;我现在已将此要求添加到问题中。
  • @user181813 将ViewBoxMaxWidth 属性设置为500。随着StackPanel 增长超过500,ViewBox 将拉伸它以适应。这将导致您正在寻找的东西。
  • 好吧,我试着写这个:&lt;ItemsPanelTemplate x:Key="panelTemplate"&gt;&lt;Viewbox MaxWidth="500"&gt; &lt;StackPanel Orientation="Horizontal" /&gt;&lt;/Viewbox&gt;&lt;/ItemsPanelTemplate&gt;。我得到了这个例外:“ItemsPanelTemplate 的 VisualTree 必须包含一个面板。'System.Windows.Controls.Viewbox' 不是一个面板。”
  • 您的答案几乎符合我的要求 - 唯一的问题是当您第一次单击按钮时字体大于 13。我已将问题更新为要求最大字体大小为 13。
  • 不幸的是,更改 HorizontalAlignment 似乎并不能解决大字体问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-07
  • 2020-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-04
相关资源
最近更新 更多