WPF: Grid with column/row margin/padding?

前端 未结 15 1068
梦毁少年i
梦毁少年i 2020-12-07 23:57

Is it easily possible to specify a margin and/or padding for rows or columns in a WPF Grid?

I could of course add extra columns to space things out,

相关标签:
15条回答
  • 2020-12-08 00:32

    I had similar problem recently in two column grid, I needed a margin on elements in right column only. All elements in both columns were of type TextBlock.

    <Grid.Resources>
        <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource OurLabelStyle}">
            <Style.Triggers>
                <Trigger Property="Grid.Column" Value="1">
                    <Setter Property="Margin" Value="20,0" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>
    
    0 讨论(0)
  • 2020-12-08 00:36

    You could write your own GridWithMargin class, inherited from Grid, and override the ArrangeOverride method to apply the margins

    0 讨论(0)
  • 2020-12-08 00:36

    One possibility would be to add fixed width rows and columns to act as the padding / margin you are looking for.

    You might also consider that you are constrained by the size of your container, and that a grid will become as large as the containing element or its specified width and height. You could simply use columns and rows with no width or height set. That way they default to evenly breaking up the total space within the grid. Then it would just be a mater of centering your elements vertically and horizontally within you grid.

    Another method might be to wrap all grid elements in a fixed with single row & column grid that has a fixed size and margin. That your grid contains fixed width / height boxes which contain your actual elements.

    0 讨论(0)
提交回复
热议问题