WPF:将宽度(和高度)设置为百分比值 [英] WPF: Setting the Width (and Height) as a Percentage Value

查看:298
本文介绍了WPF:将宽度(和高度)设置为百分比值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想要一个 TextBlockWidth 等于它的父容器的 Width(即,从一侧拉伸到另一侧)或它的父容器 Width 的百分比,如何在 XAML 中完成此操作而不指定绝对值?

Say I want a TextBlock to have its Width equal to it's Parent container's Width (ie, stretch from side to side) or a percentage of it's Parent Container Width, how can I accomplish this in XAML without specifying absolute values?

我想这样做是为了如果以后扩展父容器容器(它的Width"增加),它的子元素"也将自动扩展.(基本上,就像在 HTML 和 CSS 中一样)

I want to do this so that if the Parent Container container is later on expanded (its' Width increased), its' Child Elements will also be expanded automatically. (basically, like in HTML and CSS)

推荐答案

将其拉伸到与父容器相同大小的方法是使用属性:

The way to stretch it to the same size as the parent container is to use the attribute:

 <Textbox HorizontalAlignment="Stretch" ...

这将使 Textbox 元素水平拉伸并水平填充所有父空间(实际上这取决于您使用的父面板,但应该适用于大多数情况).

That will make the Textbox element stretch horizontally and fill all the parent space horizontally (actually it depends on the parent panel you're using but should work for most cases).

百分比只能与网格单元格值一起使用,因此另一种选择是创建一个网格并将您的文本框放在具有适当百分比的单元格中.

Percentages can only be used with grid cell values so another option is to create a grid and put your textbox in one of the cells with the appropriate percentage.

这篇关于WPF:将宽度(和高度)设置为百分比值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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