C# 动态减小字体大小以避免溢出
我编写了一个带有C# 动态减小字体大小以避免溢出,c#,wpf,overflow,itemscontrol,font-size,C#,Wpf,Overflow,Itemscontrol,Font Size,我编写了一个带有按钮和项控件的玩具WPF应用程序。每次单击按钮,字符串“AnotherWord”将添加到项控件。现在,ItemsControl显示为具有固定宽度(500像素)的水平方向的StackPanel。这意味着当您单击按钮一定次数(实际上是六次)时,新添加的字符串将被剪裁,如下所示: “另一个词,另一个词,另一个词,另一个词” 当FontSize为13时会发生这种情况;如果你把它降到12.7,那么第六次出现“另一个词”还有空间。我的问题是:有没有一种方法可以在运行时进行调整,从而避免溢出
按钮和项控件的玩具WPF应用程序。每次单击按钮
,字符串“AnotherWord”将添加到项控件
。现在,ItemsControl
显示为具有固定宽度(500像素)的水平方向的StackPanel
。这意味着当您单击按钮一定次数(实际上是六次)时,新添加的字符串将被剪裁,如下所示:
“另一个词,另一个词,另一个词,另一个词”
当FontSize
为13时会发生这种情况;如果你把它降到12.7,那么第六次出现“另一个词”还有空间。我的问题是:有没有一种方法可以在运行时进行调整,从而避免溢出
编辑:
在问题的上下文中,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");
}
}
}
//MainWindow.xaml.cs
使用System.Collections.ObjectModel;
使用System.Windows;
名称空间字体大小
{
公共部分类主窗口
{
公共主窗口()
{
初始化组件();
MyStrings=新的ObservableCollection();
}
公共可观测集合MyStrings
{
获取{return(observateCollection)GetValue(MyStringsProperty);}
set{SetValue(MyStringsProperty,value);}
}
私有静态只读DependencyProperty MyStringsProperty=
DependencyProperty.Register(“myString”、typeof(ObservableCollection)、typeof(Window));
私有无效按钮\u单击(对象发送者,路由目标e)
{
MyStrings.Add(“另一个词”);
}
}
}
将您的项控件
放在一个文件夹中,并使用以下属性:
- 最大宽度
- 最大高度
- 伸展
- 拉伸方向
编辑
并删除堆栈面板
的宽度
和高度
属性
编辑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>
将您的ItemsControl
放在中,并使用以下属性:
- 最大宽度
- 最大高度
- 伸展
- 拉伸方向
编辑
并删除堆栈面板
的宽度
和高度
属性
编辑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>
此处讨论:@paul your链接只讨论如何计算大小。虽然这是需要做的一件事,但它并不是这个问题的答案。就我个人而言,我想知道是否有人有一个实用工具可以做到这一点。WPF布局引擎可以通过扩展和收缩视觉元素来进行流畅的布局。但是,调整文本元素的字体大小并不是该机制的一部分。您必须提出自己的算法。此处讨论:@paul您的链接只讨论如何计算大小。虽然这是需要做的一件事,但它并不是这个问题的答案。就我个人而言,我想知道是否有人有一个实用工具可以做到这一点。WPF布局引擎可以通过扩展和收缩视觉元素来进行流畅的布局。但是,调整文本元素的字体大小并不是该机制的一部分。你必须想出你自己的算法。好吧,在这个问题的上下文中,我们不能使用超过500像素的像素。对不起,没有说清楚;我现在已将此要求添加到问题中。@user181813将ViewBox
的MaxWidth
属性设置为500。当StackPanel
增长超过500时,ViewBox
会将其拉伸到合适的位置。这将产生您正在寻找的结果。好吧,我试着写这个:
。我得到了一个例外:“ItemsPanelTemplate的VisualTree必须包含一个面板。“System.Windows.Controls.Viewbox”不是一个面板。”您的回答几乎符合我的要求-唯一的问题是,当您单击按钮的前几次时,字体大于13。我已经更新了这个问题,要求最大字体大小为13。不幸的是,更改水平对齐方式似乎无法解决大字体大小的问题。在这个问题中,我们不能使用超过500像素的字体。对不起,没有说清楚;我现在已将此要求添加到问题中。@user181813将ViewBox
的MaxWidth
属性设置为500。当StackPanel
增长超过500时,ViewBox
会将其拉伸到合适的位置。这将产生您正在寻找的结果。好吧,我试着写这个:
。我得到了一个例外:“ItemsPanelTemplate的VisualTree必须包含一个面板。“System.Windows.Controls.Viewbox”不是一个面板。”您的回答几乎符合我的要求-唯一的问题是,当您单击按钮的前几次时,字体大于13。我已将问题更新为要求最大字体大小为13。不幸的是,更改水平对齐方式似乎无法解决大字体的问题。