如何自动缩放一组控件的字体大小?

2024-03-03

我在 WPF 网格中有一些 TextBlock,我想根据它们的可用宽度/高度进行缩放。当我搜索自动缩放字体大小时,典型的建议是将 TextBlock 放入 ViewBox 中。

所以我这样做了:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

    <Viewbox MaxHeight="18" Grid.Column="0" Stretch="Uniform" Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <TextBlock Text="{Binding Text1}" />
    </Viewbox>

    <Viewbox MaxHeight="18" Grid.Column="1" Stretch="Uniform" Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <TextBlock Text="{Binding Text2}" />
    </Viewbox>

    <Viewbox MaxHeight="18" Grid.Column="2" Stretch="Uniform" Margin="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <TextBlock Text="{Binding Text3}" />
    </Viewbox>
</Grid>

它会自动缩放每个 TextBlock 的字体。然而,这看起来很有趣,因为如果其中一个 TextBlock 具有较长的文本,那么它将采用较小的字体,而其相邻的网格元素将采用较大的字体。我希望字体大小按组缩放,如果我可以为一组控件指定“SharedSizeGroup”以自动调整字体大小,也许会很好。

e.g.

第一个文本块文本可能是“3/26/2013 10:45:30 AM”,第二个 TextBlocks 文本可能是“FileName.ext”。如果它们横跨窗口的宽度,则用户开始将窗口的大小调整得越来越小。日期将开始使其字体小于文件名,具体取决于文件名的长度。

理想情况下,一旦其中一个文本字段开始调整字体磅值,它们就会全部匹配。有没有人想出一个解决方案,或者可以给我一个机会,告诉我如何让它发挥作用?如果它需要自定义代码,那么希望我们/我可以将其重新打包到自定义混合或附加行为中,以便将来可以重复使用。我认为这是一个非常普遍的问题,但我无法通过搜索找到任何相关内容。


Update我尝试了马蒂厄的建议,它有点有效,但它有一些副作用:

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="270" Width="522">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Rectangle Grid.Row="0" Fill="SkyBlue" />

        <Viewbox Grid.Row="1" MaxHeight="30"  Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="col"/>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="col"/>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="col"/>
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0" Text="SomeLongText" Margin="5" />
                <TextBlock Grid.Column="1" Text="TextA" Margin="5" />
                <TextBlock Grid.Column="2" Text="TextB" Margin="5" />

            </Grid>
        </Viewbox>
    </Grid>
</Window>

老实说,缺少比例列对我来说可能没问题。我不介意自动调整列的大小以巧妙地利用空间,但它必须跨越窗口的整个宽度。

请注意,如果没有 maxsize,在此扩展示例中文本太大:

<Window x:Class="WpfApplication6.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="270" Width="522">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Rectangle Grid.Row="0" Fill="SkyBlue" />

    <Viewbox Grid.Row="1"  Stretch="Uniform" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="col"/>
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" Text="SomeLongText" Margin="5" />
            <TextBlock Grid.Column="1" Text="TextA" Margin="5" />
            <TextBlock Grid.Column="2" Text="TextB" Margin="5" />

        </Grid>
    </Viewbox>
</Grid>

在这里,我想限制字体的大小,这样就不会浪费垂直窗口的空间。我期望输出左、中、右对齐,字体尽可能大到所需的最大尺寸。


@阿达拜伦

您提出的解决方案还不错(而且是最好的),但它确实有一些局限性。例如,最初我希望我的列成比例(第二个应该居中)。例如,我的 TextBlocks 可能会标记图表的起点、中心和终点对齐很重要的地方。

<Window x:Class="WpfApplication6.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:b="clr-namespace:WpfApplication6.Behavior"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Rectangle Grid.Row="0" Fill="SkyBlue" />
        <Line X1="0.5" X2="0.5" Y1="0" Y2="1" Stretch="Fill" StrokeThickness="3" Stroke="Red" />

        <Grid Grid.Row="1">

            <i:Interaction.Behaviors>
                <b:MoveToViewboxBehavior />
            </i:Interaction.Behaviors>

            <Viewbox Stretch="Uniform" />
            <ContentPresenter >
                <ContentPresenter.Content>
                    <Grid x:Name="TextBlockContainer">
                        <Grid.Resources>
                            <Style TargetType="TextBlock" >
                                <Setter Property="FontSize" Value="16" />
                                <Setter Property="Margin" Value="5" />
                            </Style>
                        </Grid.Resources>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"  />
                            <ColumnDefinition Width="*"  />
                            <ColumnDefinition Width="*"  />
                            <ColumnDefinition Width="*"  />
                            <ColumnDefinition Width="*"  />
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0" Text="SomeLongText" VerticalAlignment="Center" HorizontalAlignment="Center" />
                        <TextBlock Grid.Column="2" Text="TextA" HorizontalAlignment="Center" VerticalAlignment="Center" />
                        <TextBlock Grid.Column="4" Text="TextB" HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Grid>
                </ContentPresenter.Content>
            </ContentPresenter>
        </Grid>
    </Grid>
</Window>

这是结果。请注意,它不知道它很早就被剪裁了,然后当它替换 ViewBox 时,它看起来好像 Grid 默认为列大小“自动”并且不再居中对齐。


我想编辑我已经提供的答案,但后来决定发布一个新答案更有意义,因为这实际上取决于我更喜欢​​的要求。这可能更适合艾伦的想法,因为

  • 中间的文本块保留在窗口的中间
  • 由于高度剪切而调整字体大小
  • 比较通用一点
  • 不涉及视图框

enter image description here

The 另一个 https://stackoverflow.com/a/15952125/385995优点是

  • 文本块的空间分配更有效(没有不必要的边距)
  • 文本块可能有不同的字体大小

我还在 StackPanel/DockPanel 类型的顶部容器中测试了该解决方案,表现良好。

请注意,通过调整列/行宽度/高度(自动/星号),您可以获得不同的行为。因此,也可以将所有三个文本块列星形化,但这意味着宽度剪切确实会更早发生,并且有更多边距。或者,如果网格所在的行是自动调整大小的,则永远不会发生高度裁剪。

Xaml:

<Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
            xmlns:beh="clr-namespace:WpfApplication1.Behavior"
            Title="MainWindow" Height="350" Width="525">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.9*"/>
            <RowDefinition Height="0.1*" />
        </Grid.RowDefinitions>

        <Rectangle Fill="DarkOrange" />

        <Grid x:Name="TextBlockContainer" Grid.Row="1" >
            <i:Interaction.Behaviors>
                <beh:ScaleFontBehavior MaxFontSize="32" />
            </i:Interaction.Behaviors>
            <Grid.Resources>
                <Style TargetType="TextBlock" >
                    <Setter Property="Margin" Value="5" />
                    <Setter Property="VerticalAlignment" Value="Center" />
                </Style>
            </Grid.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"  />
                <ColumnDefinition Width="Auto"  />
                <ColumnDefinition Width="*"  />
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" Text="SomeLongText" />
            <TextBlock Grid.Column="1" Text="TextA" HorizontalAlignment="Center"  />
            <TextBlock Grid.Column="2" Text="TextB" HorizontalAlignment="Right"  />
        </Grid>
    </Grid>
</Window>

缩放字体行为:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Media;
using WpfApplication1.Helpers;

namespace WpfApplication1.Behavior
{
    public class ScaleFontBehavior : Behavior<Grid>
    {
        // MaxFontSize
        public double MaxFontSize { get { return (double)GetValue(MaxFontSizeProperty); } set { SetValue(MaxFontSizeProperty, value); } }
        public static readonly DependencyProperty MaxFontSizeProperty = DependencyProperty.Register("MaxFontSize", typeof(double), typeof(ScaleFontBehavior), new PropertyMetadata(20d));

        protected override void OnAttached()
        {
            this.AssociatedObject.SizeChanged += (s, e) => { CalculateFontSize(); };
        }

        private void CalculateFontSize()
        {
            double fontSize = this.MaxFontSize;

            List<TextBlock> tbs = VisualHelper.FindVisualChildren<TextBlock>(this.AssociatedObject);

            // get grid height (if limited)
            double gridHeight = double.MaxValue;
            Grid parentGrid = VisualHelper.FindUpVisualTree<Grid>(this.AssociatedObject.Parent);
            if (parentGrid != null)
            {
                RowDefinition row = parentGrid.RowDefinitions[Grid.GetRow(this.AssociatedObject)];
                gridHeight = row.Height == GridLength.Auto ? double.MaxValue : this.AssociatedObject.ActualHeight;
            }

            foreach (var tb in tbs)
            {
                // get desired size with fontsize = MaxFontSize
                Size desiredSize = MeasureText(tb);
                double widthMargins = tb.Margin.Left + tb.Margin.Right;
                double heightMargins = tb.Margin.Top + tb.Margin.Bottom; 

                double desiredHeight = desiredSize.Height + heightMargins;
                double desiredWidth = desiredSize.Width + widthMargins;

                // adjust fontsize if text would be clipped vertically
                if (gridHeight < desiredHeight)
                {
                    double factor = (desiredHeight - heightMargins) / (this.AssociatedObject.ActualHeight - heightMargins);
                    fontSize = Math.Min(fontSize, MaxFontSize / factor);
                }

                // get column width (if limited)
                ColumnDefinition col = this.AssociatedObject.ColumnDefinitions[Grid.GetColumn(tb)];
                double colWidth = col.Width == GridLength.Auto ? double.MaxValue : col.ActualWidth;

                // adjust fontsize if text would be clipped horizontally
                if (colWidth < desiredWidth)
                {
                    double factor = (desiredWidth - widthMargins) / (col.ActualWidth - widthMargins);
                    fontSize = Math.Min(fontSize, MaxFontSize / factor);
                }
            }

            // apply fontsize (always equal fontsizes)
            foreach (var tb in tbs)
            {
                tb.FontSize = fontSize;
            }
        }

        // Measures text size of textblock
        private Size MeasureText(TextBlock tb)
        {
            var formattedText = new FormattedText(tb.Text, CultureInfo.CurrentUICulture,
                FlowDirection.LeftToRight,
                new Typeface(tb.FontFamily, tb.FontStyle, tb.FontWeight, tb.FontStretch),
                this.MaxFontSize, Brushes.Black); // always uses MaxFontSize for desiredSize

            return new Size(formattedText.Width, formattedText.Height);
        }
    }
}

视觉助手:

public static List<T> FindVisualChildren<T>(DependencyObject obj) where T : DependencyObject
{
    List<T> children = new List<T>();
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
    {
        var o = VisualTreeHelper.GetChild(obj, i);
        if (o != null)
        {
            if (o is T)
                children.Add((T)o);

            children.AddRange(FindVisualChildren<T>(o)); // recursive
        }
    }
    return children;
}

public static T FindUpVisualTree<T>(DependencyObject initial) where T : DependencyObject
{
    DependencyObject current = initial;

    while (current != null && current.GetType() != typeof(T))
    {
        current = VisualTreeHelper.GetParent(current);
    }
    return current as T;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何自动缩放一组控件的字体大小? 的相关文章

  • C#/WPF:将 Datagrid 中的 Combobox ItemSource 绑定到 DataContext 外部的元素

    我想做以下事情 public List
  • 将 KeyUp 作为参数传递 WPF 命令绑定文本框

    我有一个文本框 KeyUp 事件触发器连接到 WPF 中的命令 我需要将按下的实际键作为命令参数传递 该命令执行得很好 但处理它的代码需要知道按下的实际键 记住这可能是一个回车键或不仅仅是一个字母的任何键 所以我无法从 TextBox te
  • 如何将 MouseDown 事件放入样式中?

    这有效 XAML
  • 谁应该在 MvvmCross 中创建视图模型实例

    澄清一下 我知道 MvvmCross 在创建视图模型的位置和方式方面非常灵活 我的问题更多的是关于适当的关注点分离 以简化复杂的跨平台应用程序的设计 假设我们有一个包含客户列表和客户详细信息的应用程序 在 iPad 和 Surface 上
  • 如何使用 MVVM 更新 WPF 中编辑的数据? [复制]

    这个问题在这里已经有答案了 我正在为聊天应用程序构建 UI 设计 在尝试更新所选联系人的消息时遇到问题 选择现有联系人 选择编辑选项 然后编辑其属性 例如用户名和图像 后 唯一进行的更改是联系人的用户名和图像 我仍然想更改 MessageM
  • 如何使用 wpf webbrowser 将数据发布到 Web 服务器

    我想从数据库获取数据并使用它来让用户登录到网站 我有一个包含 Web 浏览器控件的 wpf 页面 我有这样的代码 用于将用户登录到用 php 编写的网站
  • Windows 8 和 LOB 应用程序

    为 Windows 8 构建业务线应用程序的推荐方法是什么 例如复杂的定制财务应用程序 它们显然不适合 Metro 风格 因此可能会成为常规桌面应用程序 那么建议使用WPF吗 有 vNext 吗 我作为开发人员构建的应用程序类型是桌面风格的
  • 使用(linq to sql)更新错误

    我有两个表 通过外键 CarrierID 绑定 Carrier CarrierID CarrierName CarrierID 1 CarrierName DHL CarrierID 2 CarrierName Fedex Vendor V
  • 自定义 WPF 工具提示

    我想创建一个 WPF 工具提示 其中包含工具提示标题的标签 然后创建一个包含更详细文本的文本块 我在资源字典中创建了以下样式
  • 禁止/阻止选择 wpf 中禁用的组合框项目

    我正在编写一个应用程序 其中我想禁用其中的一些项目ComboBox并且还想禁止 阻止选择禁用的项目 请注意ComboBox在主窗口中有另一个 ComboBox 作为 ComboBox Item init 在运行时由DataTemplateS
  • 从 WPF 绑定列表框中删除项目

    我有一个带有 ListBox 称为 listMyItems 的 WPF 应用程序 它已成功绑定到我创建的 MyItems 类 我有一个名为 currentMyItems 的 MyItems 列表 然后将其作为 ItemSource 分配给
  • 如何在 WPF 数据网格中一起选择所有复选框

    我的 wpf 数据网格是
  • 如何按 z-index 对 Windows 进行排序?

    如果我枚举窗口Application Current Windows 对于任意两个窗口 我如何判断哪一个 更接近 即具有更大的 z index 或者 换句话说 我如何按 z 索引对这些窗口进行排序 您无法从 WPF 获取 Window 的
  • WPF 无法从 url 检索 WebP 图像?

    我无法从网址检索图像 以前 在设置 HttpClient 标头之前 我根本无法连接到该站点 我可以从其他来源检索图像 但不能从这个特定来源检索图像 检索图像的代码 var img new BitmapImage img BeginInit
  • WPF XAML 绑定和 CurrentCulture 显示

    当 CurrentCulture 更改时 我发现 XAML 文档出现一些无效行为 当我在窗口中有一些这样的元素时
  • 弹簧质量系统的阻尼效果(或者这是 ElasticEase?)

    我试图在代码中模拟动画效果 几乎任何语言都可以 因为它看起来是数学而不是语言 本质上 它是质量弹簧系统的仿真 我一直在研究 WPF Silverlight 的ElasticEase这似乎与我正在寻找的非常接近 但又不完全一样 首先 这就是我
  • 如何从注册表获取安装的软件路径?

    我需要替换安装在任何驱动器 如 C D E 中的文件 我想从注册表中找到安装的文件路径并将该文件替换为其他文件 软件将安装在任何驱动器中 我想替换文件 我正在使用这段代码 如何使用注册表查找安装的文件路径并替换为 C 中的其他文件 stri
  • SQL Server CE 不兼容的数据库版本

    我有一个 SQL Server CE 4 0 数据库 sdf文件 当我尝试从我的应用程序 WPF 对数据库进行查询时 出现以下错误 数据库版本不兼容 如果这是兼容文件 请运行修复 其他情况请参考文档 数据库版本 4000000 请求的版本
  • 如何制作wpf倒计时器?

    我想创建 wpf 倒数计时器 将结果显示为hh mm ss进入文本框 我将感谢任何人的帮助 您可以使用DispatcherTimer class msdn http msdn microsoft com en US library syst
  • 我对 MVVM 模式有一些疑问

    我叫 Jes s 来自西班牙 是一名 NET 开发人员 几天前我刚刚发现了这个伟大的网络 我有一些关于 MVVM 模式的问题 如果您能回答我 我将很高兴 我三个月前开始使用 WPF 并且学习了 MVP 模式 MVP 非常好 因为您可以很好地

随机推荐