margin 属性 到底是控件边缘和窗体边缘的距离,还是控件和空间之间的距离?我试了发现,当控件在Grid上时,margin是边缘和窗体边缘的距离,把控件放到ListBox或者GroupBox里面, margin变成了控件和上个控件之间的距离,这是为什么?

解决方案 »

  1.   

    盒子模型,IE和Firefox等不同浏览器中不太一样。
      

  2.   

    margin表示同父容器边缘的距离,父容器可以是窗体或其他可容性控件:如Panel、GroupBox等。
      

  3.   

    比方这样个代码段:
    <GroupBox Header="Experience" Height="200" HorizontalAlignment="Left" Margin="29,174,0,0" Name="yearsExperience" VerticalAlignment="Top" Width="258" Style="{StaticResource bellRingersFontStyle}">
    <RadioButton Content="Up to 1 year" Height="16" Name="novice" Width="120" Margin="0, 10, 0, 0" />
    <RadioButton Content="1 to 4 years" Height="16" Name="intermediate" Width="120" Margin="0, 20, 0, 0" />
    <RadioButton Content="5 to 9 years" Height="16" Name="experienced" Width="120" Margin="0, 20, 0, 0" />
    <RadioButton Content="10 or more" Height="16" Name="accomplished" Width="120" Margin="0, 20, 0, 0" />
    </GroupBox>
    4个RadioButton父容器都是GroupBox,按理他们的margin应该是和父容器也就是GroupBox边缘的距离,但实际上却是RadioButton和上一个RadioButton的距离。
      

  4.   

    MSDN里是这么说的:
    在窗体上精确定位控件对于许多应用程序都非常重要。System.Windows.Forms 命名空间提供了许多布局功能来实现此目的。其中最重要的两个是 Margin 和 Padding 属性。Margin 属性定义控件周围的空白,使其他控件和该控件边框保持指定距离。Padding 属性定义控件内部的空白,使控件的内容(例如其 Text 属性值)与该控件边框保持指定距离。 所以我刚才的说法有误,以此为准。
      

  5.   

    我上传了MSDN里的一张图片,你看下:
      

  6.   

    也不对啊,在Grid上就是缘和窗体边缘的距离,如果你把刚才4个RadioButton放在Grid上,用同样的margin,就会相互覆盖。
      

  7.   

    margin 是外填充 
    在不同地方, 他们外填充起到作用不同。
      

  8.   

    margin是外部的边框距离。看看这个,很详细
      

  9.   

    http://www.w3school.com.cn/css/index.asp
      

  10.   

    我查了msdn文档,对margin 的定义是这样的 “The Margin property describes the distance between an element and its child or peers。”也就说margin属性指控件和父容器之间的距离或者同级控件之间的距离,但我的问题是,怎么决定什么情况下margin是件和父容器之间的距离?什么情况下是同级控件之间的距离?