<Window x:Class="_2_3_线程示例.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid CellPadding="5 5 5 5 ">//这里为我自己比喻的属性 谢谢
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Button Content="在行1左" Grid.Row="0" Grid.Column="0"></Button>
        <Button Content="在行1右" Grid.Row="0" Grid.Column="1"></Button>
        <Button Content="在行2左" Grid.Row="1" Grid.Column="0"></Button>
        <Button Content="在行2右" Grid.Row="1" Grid.Column="1"></Button>
    </Grid></Window>

解决方案 »

  1.   

    参考
    http://www.cnblogs.com/libaoheng/archive/2011/11/20/2255963.html
      

  2.   


        <Grid>
            <Grid.Resources>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Margin" Value="5 5 5 5"/>
                </Style>
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Content="在行1左" Grid.Row="0" Grid.Column="0"></Button>
            <Button Content="在行1右" Grid.Row="0" Grid.Column="1"></Button>
            <Button Content="在行2左" Grid.Row="1" Grid.Column="0"></Button>
            <Button Content="在行2右" Grid.Row="1" Grid.Column="1"></Button>
        </Grid>
      

  3.   


    <Style TargetType="{x:Type Button}">                 
      <Setter Property="Margin" Value="5 5 5 5"/>             
    </Style>这样为设置Button的Margin,但我想要的是设置Grid单元格的Padding
    谢谢
      

  4.   

    其实好像是没有办法做到你说的要求。如果Grid单元格中的内容不确定,要么用命名style (x:Key="xx")来控制,要么在每个格子外面再包个panel什么的。
    还有一个办法是在每行每列之间插入一个假的空白行、列,不过也肯定和你的要求不符。
    再来就是自己去改程序扩展了,参考这篇文章:
    http://www.codeproject.com/Articles/107468/WPF-Padded-Grid
    其它方法就不知道了,看看别人有没有好办法。
      

  5.   

    编写一个你自己的 GridWithMargin 类,从 Grid 继承,重写 ArrangeOverride 方法。