0

I am creating a form in WPF using Grid to design my form.

Here is my sample code.

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width=".6*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Label Grid.Column="0">Company Name</Label>
    <TextBox Grid.Column="1"></TextBox>

    <Label Grid.Column="0" Grid.Row="1">Company Name</Label>
    <TextBox Grid.Column="1" Grid.Row="1"></TextBox>

    <Label Grid.Column="0" Grid.Row="2">Company Name</Label>
    <TextBox Grid.Column="1" Grid.Row="2"></TextBox>
</Grid>

But this is the output of my form.

enter image description here

I want to give some space from top to textbox, so that it looks better.

Is there any way, I can give top margin to textbox as I don't want to write Margin="0,20,0,0" on each textbox?

Or should I use other layout control instead of Grid to design my form?

Please suggest me.

Community
  • 1
  • 1
Programmer
  • 398
  • 1
  • 9
  • 33
  • [This answer](https://stackoverflow.com/a/53015911/424129) demonstrates an attached property that will you set a fixed margin on all children of a `Grid`, `StackPanel`, etc. – 15ee8f99-57ff-4f92-890c-b56153 Jun 03 '19 at 18:28
  • @EdPlunkett: No offence, but that is horrifying answer to set just `margin`, it's better to switch to window form. – Programmer Jun 03 '19 at 18:33
  • @EdPlunkett: that's not the reason. I have just started learning wpf & doesn't have any clue about attached properties. I think it's good to learn about attached properties before using your answer. Thank you. – Programmer Jun 03 '19 at 18:38
  • 1
    It makes no sense to port an application from WPF to winforms. – 15ee8f99-57ff-4f92-890c-b56153 Jun 03 '19 at 18:43
  • @EdPlunkett: quick question, using Grid is best way to design form or you use any other way in projects? – Programmer Jun 03 '19 at 18:43
  • Grid is often appropriate for more complicated grid layouts. Sometimes UniformGrid is more appropriate for very simple, regular grid layouts; often, StackPanel or WrapPanel is the right way to do things. Very often, you'll have StackPanels and/or WrapPanels *within* a parent grid. It depends on the details of what you're trying to do. None of them allow you to set a margin for all children, but the linked attached property can be applied to any of them. – 15ee8f99-57ff-4f92-890c-b56153 Jun 03 '19 at 18:48

0 Answers0