Skip to main content
 首页 » 编程设计

c#之动态减小 FontSize 以避免溢出

2024年02月23日15bjzhanghao

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

"AnotherWord AnotherWord AnotherWord AnotherWord AnotherWord AnotherWo"

当 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"); 
        } 
    } 
} 

请您参考如下方法:

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

  • 最大宽度
  • 最大高度
  • 拉伸(stretch)
  • 拉伸(stretch)方向

编辑

并删除 StackPanel 的 Width 和 Height 属性。

编辑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 的水平对齐方式,使其不会被拉伸(stretch)以填充网格。我放了“中心”,用你想要的任何东西代替。

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