WPF / XAML将元素的宽度绑定到屏幕大小的一小部分 [英] WPF/XAML bind width of an element to a fraction of screen size
问题描述
Grid = 2x2
行1高度=屏幕高度的2/3
行2高度=屏幕高度的1/3
行1宽度=屏幕宽度的2/3
行2宽度=屏幕宽度的1/3
我认为正确地将整个宽度绑定到列定义:
< ColumnDefinition Width ={Binding ElementName = Window1,Path = Width}/>
但是我不知道该怎么做,就是通过对数据绑定...这是甚么可能的?我觉得这是我应该能够编码到XAML中的东西,而不必以编程方式实现,但是我在UI设计方面没有什么经验:(我想要的是:
< ColumnDefinition Width ={Binding ElementName = Window1,Path = Width} * 2/3/>
但无效
如果屏幕调整大小,我应该正在编写一个重新布局UI元素的函数?我觉得这是多余的,还是有一些简单的做法,我不知道什么?任何输入欢迎!谢谢!
听起来你只想使用星号大小:
< Grid>
< Grid.RowDefinitions>
< RowDefinition Height =2 */>
< RowDefinition Height =1 */>
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition宽度=2 */>
< ColumnDefinition Width =1 */>
< /Grid.ColumnDefinitions>
这将总是给第一行的高度和第二行的1/3高度,宽度的第一列2/3和宽度的第二列1/3。它将基于Grid的大小,但如果Grid是Window的唯一子节点,那么它将与Window的大小相同。
I am writing an application in C#/WPF and am trying to figure out how to databind the width of the grids column definitions to a fraction of the screen width. Is this possible? Essentially I want something like this:
Grid = 2x2
Row 1 Height = 2/3 of screen height
Row 2 Height = 1/3 of screen height
Row 1 Width = 2/3 of screen width
Row 2 Width = 1/3 of screen width
I think that this correctly binds the full width to a column definition:
<ColumnDefinition Width="{Binding ElementName=Window1, Path=Width}"/>
but what I don't know how to do is perform an operation on the value it gets through the databinding... is this even possible? I feel like this is something I should be able to code into the XAML and not have to implement programmatically but I have little experience with UI design :( I would want something like:
<ColumnDefinition Width="{Binding ElementName=Window1, Path=Width} * 2 / 3"/>
but that is invalid
Should I just be writing a function to re-layout UI elements whenever the screen resizes? I feel like that is redundant... or is there some easy way of doing this that I don't know about? Any input welcome! Thanks!
It sounds like you just want to use star sizing:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
This will always give the first row 2/3 of the height and the second row 1/3 of the height, and the first column 2/3 of the width and the second column 1/3 of the width. It will be based on the size of the Grid, but if the Grid is the only child of the Window then that will be the same as the size of the Window.
这篇关于WPF / XAML将元素的宽度绑定到屏幕大小的一小部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!