【问题标题】:WPF: Textblock Font size should be even when its size changeWPF:文本块字体大小应该是即使它的大小改变
【发布时间】:2012-05-16 21:02:33
【问题描述】:

我在我的应用程序中创建了一个自定义按钮,如下所示

<Button x:Class="MyApp.ButtonMainMenuSubCat"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="536">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Grid>
                <Grid Name="gridParent">
                    <Image Name="imgTransparent" Source="/MyApp;component/Resources/LeftCategoryTransparent.png" Stretch="Fill" Margin="0,0,0,0"  />
                    <Image Name="Part_Pressed" Source="/MyApp;component/Resources/PressedMainScreenSubMenu.png" Stretch="Fill" Margin="0,0,0,4"  Visibility="Hidden"/>
                    <Image Name="Focused" Source="/MyApp;component/Resources/MainSubMenuFocus.png" Margin="-3,0,-3,3"   Stretch="Fill" Visibility="Hidden" />
                    <Image Name="Seperator" Source="/MyApp;component/Resources/MainSubMenuSeperator.png" Margin="5,0,5,-1"   Stretch="Uniform" VerticalAlignment="Bottom"/>

                    <TextBlock Name="lblTitle" Text="{TemplateBinding Content}" HorizontalAlignment="Left"  Foreground="Gray" FontWeight="{TemplateBinding FontWeight}"   FontSize="24"  Margin="10" VerticalAlignment="Center" TextWrapping="WrapWithOverflow" TextAlignment="Left"/>
                </Grid>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <!--<Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>-->

                </Trigger>
                <Trigger Property="IsFocused" Value="True">
                    <Setter TargetName="Focused" Property="Visibility" Value="Visible"/>
                    <Setter TargetName="lblTitle" Property="Foreground" Value="#f8cb1c" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>

在我的应用程序中,我调用 Web 服务,根据项目的数量,我为每个项目创建按钮,最后在 StackPanel 中添加这些按钮。它运作良好。 Stakpanel 布局在屏幕截图的左侧。现在的问题是,当我在具有不同屏幕分辨率(如 1920 * 1200)的不同机器上运行此应用程序时,字体大小似乎太小了。所以我想在容器大小改变时调整字体大小。一种选择是使用 ViewBox 但在 ViewBox 的情况下,所有按钮似乎都有不同的字体大小,并且 TextWrapping 是不可能的。

所以我的实际要求是使用 TextWrapping 增加/减少文本块的字体大小,并且所有按钮的字体大小必须均匀。

【问题讨论】:

  • 您是否考虑过在 StackPanel 上使用缩放转换?
  • 我是 WPF 新手,从不从事转换工作。您能否详细说明或提供任何代码 sn-p ?谢谢。

标签: wpf textblock font-size


【解决方案1】:

您可以根据屏幕分辨率使用 LayoutTransform,您可以放大堆栈面板或根据分辨率放大整个窗口。

对于堆栈面板:

<StackPanel>
    <StackPanel.LayoutTransform>
        <ScaleTransform  x:Name="ScaleTransform" />
    </StackPanel.LayoutTransform>
    <Button>A</Button>
    <Button>B</Button>
    <Button>C</Button>
    <Button>D</Button>
</StackPanel>

然后将 ScaleX 和 ScaleY 绑定到基于分辨率的公式。一个例子:

double scale = System.Windows.SystemParameters.PrimaryScreenHeight / 720;
//using 720p as the base screen height.

this.ScaleTransform.ScaleX = scale;
this.ScaleTransform.ScaleY = scale;

如果您使用 MVVM,您可能希望在带有一些数据绑定的 ViewModel 中执行此操作。

【讨论】:

  • 我使用您的代码,但它会缩放按钮高度。但是,您给了我方向,告诉我如何缩放文本块的字体大小。所以我的问题解决了。非常感谢。
  • 我在自定义按钮中添加以下事件 private void Button_SizeChanged(object sender, SizeChangedEventArgs e) { TextBlock lblTitle = (TextBlock)base.GetTemplateChild("lblTitle");双刻度= System.Windows.SystemParameters.PrimaryScreenHeight / 731; if (lblTitle != null) { lblTitle.FontSize = 24.5 * 比例; } }
  • LayoutTransform 也会缩放字体大小!尝试更极端的变换(2 或 3)
  • 我没有得到你。极端变换(2 或 3)是什么意思?
猜你喜欢
  • 2019-12-12
  • 1970-01-01
  • 2011-12-15
  • 2014-10-07
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 2019-03-09
相关资源
最近更新 更多