日期:2014-05-17  浏览次数:20801 次

|ZYCWPF| 在WPF当中,如何给Gird的每一行,每一列统一设置Padding呢、?

<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>

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

    <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>