Ivan
Ivan

Reputation: 1171

How to get rid of margin positioning?

I just have a general question regarding an issue that I am facing with element scaling/positioning in wpf. Beacuse I am new in wpf, I simply dragged and rescaled elements in visual preview of the application. Of course, this has set the margin to each element, and now when I resize the main window or run the app in full screen mode, everything gets messed up and overlapped. I understand now what the margin does and that it is very static. What is the best way to change this into some dynamic positioning (grid rows/columns). I just have no idea right now.

Upvotes: 1

Views: 113

Answers (2)

Patrick
Patrick

Reputation: 17973

In order to place a button in the second row and second column in a grid, you can use the following XAML code.

Worth noting in the example is that the *-definition is relative to other *-sizes, so a 1* is half the size of a 2* and so on.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="1*" />
        <RowDefinition Height="2*" /> <!-- Make this row double in height -->
        <RowDefinition Height="1*" />
        <RowDefinition Height="1*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" /> <!-- You don't need a number either -->
        <ColumnDefinition Width="*" /> <!-- All columns are the same size -->
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Button Grid.Row="1" Grid.Column="1" Content="Hello" />
</Grid>

Upvotes: 1

Dimitri Mockelyn
Dimitri Mockelyn

Reputation: 1545

You should use a Grid

In a grid, you can define row's height, column's width ... it looks like this

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="100" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="5*" />
    </Grid.RowDefinitions>
</Grid>

You should have a look at this article

Upvotes: 0

Related Questions