WPF / XAML associates element width with a fraction of screen size

I am writing an application in C # / WPF and trying to figure out how to bind the width of the grid column definitions to a fraction of the screen width. Is it possible? Essentially, I want something like this:

Grid = 2x2
Row 1 Height = 2/3 of the screen height
Row 2 Height = 1/3 of the screen height
Row 1 Width = 2/3 of the screen width
Row 2 Width = 1/3 of the screen width

I think this correctly binds the entire width to the column definition:

<ColumnDefinition Width="{Binding ElementName=Window1, Path=Width}"/>

but I don’t know how to do this, is to perform an operation on the value that it receives through data binding ... is this even possible? I feel that this is something that I should be able to code in XAML and not execute programmatically, but I have little experience with user interface design :( I would like something like:

<ColumnDefinition Width="{Binding ElementName=Window1, Path=Width} * 2 / 3"/>

but it is wrong

Should I just write a function to re-arrange the user interface elements each time I change the screen size? I feel this is redundant ... or is there some simple way to do this that I don't know about? Any input is welcome! Thank!

+3
source share
1 answer

Looks like you just want to use star size:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="2*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="2*"/>
        <ColumnDefinition Width="1*"/>
    </Grid.ColumnDefinitions>

2/3 1/3 , 2/3 1/3 . Grid, Grid Window, , .

+4

Source: https://habr.com/ru/post/1762189/


All Articles