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

标签 c# wpf overflow itemscontrol font-size

我编写了一个带有 ButtonItemsControl 的玩具 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)方向

编辑

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

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

关于c# - 动态减小 FontSize 以避免溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10192363/

相关文章:

c# - WPF:更改每个页面导航上的窗口标题

c++ - 如何检测 C++ 中的溢出?

javascript - 溢出时自动滚动内容 :none element (jQuery)

c# - 如何允许所有后续线程继续通过 "gate"

c# - RDLC 分组改变了我的排序

c# - 在多个文件中重复使用指令

WPF,让垂直拉伸(stretch)按预期工作!

c# - 在递归调用中跟踪 parent

wpf - 有什么方法可以有效地调试 WPF 数据绑定(bind)?

html - 带有悬停内容的可滚动表格