FrameworkElement.Margin Property


获取或设置元素的外边距。Gets or sets the outer margin of an element.

 property System::Windows::Thickness Margin { System::Windows::Thickness get(); void set(System::Windows::Thickness value); };
public System.Windows.Thickness Margin { get; set; }
member this.Margin : System.Windows.Thickness with get, set
Public Property Margin As Thickness

Property Value


提供元素的边距值。Provides margin values for the element. 默认值是所有属性都等于 0(零)的 ThicknessThe default value is a Thickness with all properties equal to 0 (zero).


边距是在布局创建 用户界面 (UI)user interface (UI)时此元素和其他将相邻的元素之间的空间。The margin is the space between this element and other elements that will be adjacent when layout creates the 用户界面 (UI)user interface (UI). 共享的元素可能是对等元素(例如公共父控件的集合中的其他元素),也可能是此元素的父级。Shared elements might be peer elements (such as other elements in the collection of a common parent control), or might also be this element's parent.

Margin 设置为 Thickness 结构而不是一个数字,以便可以以非对称方式设置边距。Margin is set as a Thickness structure rather than as a number so that the margin can be set asymmetrically. Thickness 结构本身支持字符串类型转换,因此,也可以 XAMLXAML 特性语法指定非对称 MarginThe Thickness structure itself supports string type conversion so that you can specify an asymmetric Margin in XAMLXAML attribute syntax also.

非零边距在元素布局的 ActualWidthActualHeight之外应用空间。A non-zero margin applies space outside the element layout's ActualWidth and ActualHeight.

对于布局中的同级元素,边距是累加的;例如,如果两个相邻的元素在相邻边缘上都设置为30,则它们之间的空间间距将为60。Margins are additive for sibling elements in a layout; for example, two adjacent elements both with a margin of 30 set on the adjoining edge would have 60 units of space between them.

如果为边距和元素内容区域分配的矩形空间不够大,则具有边距设置的元素通常不会限制指定 Margin 的大小。Elements that have margins set will not typically constrain the size of the specified Margin if the allotted rectangle space is not large enough for the margin plus the element content area. 当计算布局时,元素内容区域将受到约束。The element content area will be constrained instead when layout is calculated. 如果内容已被约束为零,则只会对边距进行约束。The only case where margins would be constrained also is if the content is already constrained all the way to zero.

XAML 特性用法XAML Attribute Usage

<object Margin="left,top,right,bottom"/>  


<object Margin="left,top"/>  


<object Margin="thicknessReference"/>  

XAML 属性元素用法XAML Property Element Usage

    <Thickness Left="left" Top="top" Right="right" Bottom="bottom"/>  


左、上、右、下left, top, right, bottom
介于0和 PositiveInfinity 之间的数值,用于指定 Thickness 结构的四个可能的维度属性。Number values between 0 and PositiveInfinity that specify the four possible dimension properties of a Thickness structure.

属性用法还会接受以对称方式和逻辑方式应用的顺序应用的缩略值。The attribute usage will also accept abbreviated values that apply in the order provided, symmetrically and logically. 例如,Margin="20" 将被解释为表示 Thickness 并将所有属性设置为20。For instance, Margin="20" will be interpreted to mean a Thickness with all properties set to 20. Margin="20,50" 将被解释为表示 LeftRightThickness 设置为20,TopBottom 设置为50。Margin="20,50" will be interpreted to mean a Thickness with Left and Right set to 20, and Top and Bottom set to 50.

Thickness 度量值的默认单位为 与设备无关的单位(1/96 英寸)device-independent unit (1/96th inch)The default unit for a Thickness measure is 与设备无关的单位(1/96 英寸)device-independent unit (1/96th inch). 还可以通过将单元类型字符串追加到任何度量值 cminpt 来指定其他单位。You can also specify other units by appending the unit type strings cm, in, or pt to any measure.

作为 XAMLXAML 属性提供的数字值不需要指定小数点(0是可接受的,无需作为0.0 提供)。Number values provided as XAMLXAML attributes need not specify decimal points (0 is acceptable, does not have to be provided as 0.0). 有关 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML) 用法的详细信息,请参阅 ThicknessFor more information on 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML) usage, see Thickness.

对现有 Thickness的对象引用。An object reference to an existing Thickness. 这可能是 }、或 } 引用。This might be a }, a , or } reference. 有关 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML) 用法的详细信息,请参阅 ThicknessFor more information on 可扩展应用程序标记语言 (XAML)Extensible Application Markup Language (XAML) usage, see Thickness.

依赖项属性信息Dependency Property Information

标识符字段Identifier field MarginProperty
元数据属性设置为 trueMetadata properties set to true AffectsMeasure

Applies to

See also