在网格中的文本框中调整字体大小 [英] Resize font in TextBox in Grid

查看:151
本文介绍了在网格中的文本框中调整字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的以下问题得到了回答,但我刚刚意识到,现在FontSize仅在一个方向上调整大小.

My question below is answered but I just realised that now the FontSize only resizes in one direction.

是否可以绑定两条路径?还是其他选择?

Is there a possibility to bind two paths? Or another option?

上一个问题 我有一个Grid [20,20],其中包含TextBox es.在这些TextBoxe中,我输入数字.当应用程序启动时,它是全屏的.如果我调整窗口的大小,则带有TexBox es的Grid也会重新调整大小.但是Font保持不变.所以我想在调整窗口大小时更改FontSize.我试过了:

Previous question I have a Grid [20,20] with TextBoxes in it. In these TextBoxes I put numbers. When the application starts up it's fullscreen. If I resize the window the Grid with the TexBoxes also resizes. But the Font stays the same. So I want to change the FontSize when the Window resizes. I tried:

 FontSize="{Binding ElementName=aTextBox, Path=Height}"

标签之间的

.但这是行不通的.用属性HeightWidth绑定GridWindow也不起作用.绑定到滑块后,FontSize会根据滑块的值而变化.有人有很好的解决方案吗?

between the TextBox tags. But that doesn't work. A binding with the Grid or Window with property Height or Width didn't work either. With binding to a slider the FontSize changes according to the the value of the slider. Does anybody have a nice solution?

推荐答案

Height属性是TextBox的初始高度. ActualHeight表示TextBox在屏幕上绘制时的高度.这样您的绑定就变成:

The Height property is the initial height of the TextBox. The ActualHeight represents the height of the TextBox as it is drawn on the screen. So your binding becomes:

<TextBox FontSize="{Binding Path=ActualHeight, RelativeSource={RelativeSource Self}, Converter={StaticResource HeightToFontSizeConverter}}" Text="12345" />

请注意,由于HeightFontSize之比不是1:1,并且文本对于TextBox而言太大,所以我使用了转换器:

Notice that I used a converter because the ratio Height to FontSize is not 1:1 and the text is too big for the TextBox:

class HeightToFontSizeConverter : IValueConverter {

  public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
    var height = (double) value;
    return .65 * height;
  }

  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
    throw new NotImplementedException();
  }

}

这篇关于在网格中的文本框中调整字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆