简体   繁体   English

WPF 文本框可调整大小的文本

[英]WPF textbox resizable text

I am trying to create resizable textboxes (so when the window is resized the textbox adjusts itself) programatically but the text inside the textbox is always very small in comparison to the textbox:我正在尝试以编程方式创建可调整大小的文本框(因此当调整窗口大小时,文本框会自行调整),但与文本框相比,文本框内的文本总是非常小:

TextBox textBox = new TextBox();
textBox.Name = name;
textBox.Text = text;
textBox.SetValue(Grid.ColumnProperty, column);
textBox.SetValue(Grid.RowProperty, row);
textBox.SetValue(Grid.ColumnSpanProperty, columnspan);
textBox.SetValue(Grid.RowSpanProperty, rowspan);

So i added a binding to keep the text at the same size as the textbox:所以我添加了一个绑定来保持文本与文本框的大小相同:

Binding b = new Binding();
b.RelativeSource = new RelativeSource(RelativeSourceMode.Self);
b.Path = new PropertyPath(TextBox.ActualHeightProperty);
textBox.SetBinding(TextBox.FontSizeProperty, b);

but when i do this the text becomes way too big for the textbox.但是当我这样做时,文本对于文本框来说变得太大了。

What am i doing wrong/missing here?我在这里做错了什么/错过了什么?

You are binding the fontsize which is pixels to actual height which is in device independent units so its apples to oranges.您将像素的字体大小绑定到设备独立单位的实际高度,因此它的苹果到橘子。

Is there any reason to not use a viewbox?有什么理由不使用视图框吗?

Something like (I'm doing this freehand so beware..no ide may have typos)类似的东西(我正在徒手做这个,所以要小心..没有ide可能有错别字)

TextBox textBox = new TextBox();
Viewbox vb = new Viewbox();

vb.Child = textbox;
vb.Stretch = Uniform;
textBox.Name = name;
textBox.Text = text;
vb.SetValue(Grid.ColumnProperty, column);
vb.SetValue(Grid.RowProperty, row);
vb.SetValue(Grid.ColumnSpanProperty, columnspan);
vb.SetValue(Grid.RowSpanProperty, rowspan);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM