WPF: Grid with column/row margin/padding?

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, but this seems like a job for padding/margins (it will give much simplier XAML). Has someone derived from the standard Grid to add this functionality?


您可以编写自己的GridWithMargin类,从Grid继承,并重写ArrangeOverride方法以应用边距


RowDefinition and ColumnDefinition are of type ContentElement , and Margin is strictly a FrameworkElement property. So to your question, "is it easily possible" the answer is a most definite no. And no, I have not seen any layout panels that demonstrate this kind of functionality.

You can add extra rows or columns as you suggested. But you can also set margins on a Grid element itself, or anything that would go inside a Grid , so that's your best workaround for now.


Use a Border control outside the cell control and define the padding for that:

    <Grid>
        <Grid.Resources >
            <Style TargetType="Border" >
                <Setter Property="Padding" Value="5,5,5,5" />
            </Style>
        </Grid.Resources>

        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Border Grid.Row="0" Grid.Column="0">
            <YourGridControls/>
        </Border>
        <Border Grid.Row="1" Grid.Column="0">
            <YourGridControls/>
        </Border>

    </Grid>


Source:

  • Original Source
  • and from Way Back Machine
  • 链接地址: http://www.djcxy.com/p/29476.html

    上一篇: 以编程方式在LinearLayout中设置边距

    下一篇: WPF:具有列/行边距/填充的网格?