AnchoredBlock.Margin 属性

定义

获取或设置元素的边距粗细。

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

属性值

Thickness

一个 Thickness 结构,它以与设备无关的像素为单位指定要应用的边距量。

默认值为统一粗细,即零 (0.0)。

示例

以下示例演示如何设置Margin元素 () Paragraph 的属性Block

<FlowDocument Background="LightSlateGray" ColumnWidth="2000">
  <Section Background="DarkMagenta" Margin="0" Padding="0">
    <Paragraph Background="White">
      <Run>Default paragraph.</Run>
    </Paragraph>
    <Paragraph Background="White">
      <Run>Default paragraph.</Run>
    </Paragraph>
    <Paragraph Background="LightBlue" Margin="50">
      <Run>This paragraph has a magin of 50 pixels set, but no padding.</Run>
    </Paragraph>
    <Paragraph Background="LightCoral" Padding="50">
      <Run>This paragraph has padding of 50 pixels set, but no margin.</Run>
    </Paragraph>
    <Paragraph Background="LightGreen" Margin="50" Padding="50">
      <Run>This paragraph has both padding and margin set to 50 pixels.</Run>
    </Paragraph>
    <Paragraph Background="White">
      <Run>Default paragraph.</Run>
    </Paragraph>
    <Paragraph Background="White">
      <Run>Default paragraph.</Run>
    </Paragraph>
  </Section>
</FlowDocument>

下图显示了此示例的呈现效果。 夸张的粗细和颜色用于插图。

屏幕快照:具有填充和边距的段落

下面的示例演示如何以编程方式设置 Margin 属性。

FlowDocument flowDoc = new FlowDocument();
Section sec = new Section();

flowDoc.Background = Brushes.LightSlateGray;
flowDoc.ColumnWidth = 2000;
sec.Background = Brushes.DarkMagenta;
sec.Padding = sec.Margin = new Thickness(0);

Paragraph defPar1 = new Paragraph(new Run("Default paragraph."));
Paragraph defPar2 = new Paragraph(new Run("Default paragraph."));
Paragraph defPar3 = new Paragraph(new Run("Default paragraph."));
Paragraph defPar4 = new Paragraph(new Run("Default paragraph."));
defPar1.Background = defPar2.Background = defPar3.Background = defPar4.Background = Brushes.White;

Paragraph marginPar = new Paragraph(new Run("This paragraph has a magin of 50 pixels set, but no padding."));
marginPar.Background = Brushes.LightBlue;
marginPar.Margin = new Thickness(50);
Paragraph paddingPar = new Paragraph(new Run("This paragraph has padding of 50 pixels set, but no margin."));
paddingPar.Background = Brushes.LightCoral;
paddingPar.Padding = new Thickness(50);
Paragraph marginPaddingPar = new Paragraph(new Run("This paragraph has both padding and margin set to 50 pixels."));
marginPaddingPar.Background = Brushes.LightGreen;
marginPaddingPar.Padding = marginPaddingPar.Margin = new Thickness(50);

sec.Blocks.Add(defPar1);
sec.Blocks.Add(defPar2);
sec.Blocks.Add(marginPar);
sec.Blocks.Add(paddingPar);
sec.Blocks.Add(marginPaddingPar);
sec.Blocks.Add(defPar3);
sec.Blocks.Add(defPar4);
flowDoc.Blocks.Add(sec);
Dim flowDoc As New FlowDocument()
Dim sec As New Section()

flowDoc.Background = Brushes.LightSlateGray
flowDoc.ColumnWidth = 2000
sec.Background = Brushes.DarkMagenta
sec.Margin = New Thickness(0)
sec.Padding = sec.Margin

Dim defPar1 As New Paragraph(New Run("Default paragraph."))
Dim defPar2 As New Paragraph(New Run("Default paragraph."))
Dim defPar3 As New Paragraph(New Run("Default paragraph."))
Dim defPar4 As New Paragraph(New Run("Default paragraph."))
defPar4.Background = Brushes.White
defPar3.Background = defPar4.Background
defPar2.Background = defPar3.Background
defPar1.Background = defPar2.Background

Dim marginPar As New Paragraph(New Run("This paragraph has a magin of 50 pixels set, but no padding."))
marginPar.Background = Brushes.LightBlue
marginPar.Margin = New Thickness(50)
Dim paddingPar As New Paragraph(New Run("This paragraph has padding of 50 pixels set, but no margin."))
paddingPar.Background = Brushes.LightCoral
paddingPar.Padding = New Thickness(50)
Dim marginPaddingPar As New Paragraph(New Run("This paragraph has both padding and margin set to 50 pixels."))
With marginPaddingPar
    .Background = Brushes.LightGreen
    .Margin = New Thickness(50)
    .Padding = marginPaddingPar.Margin
End With

sec.Blocks.Add(defPar1)
sec.Blocks.Add(defPar2)
sec.Blocks.Add(marginPar)
sec.Blocks.Add(paddingPar)
sec.Blocks.Add(marginPaddingPar)
sec.Blocks.Add(defPar3)
sec.Blocks.Add(defPar4)
flowDoc.Blocks.Add(sec)

注解

Margin 是位于元素的内容区域外部的缓冲区空间,位于元素内容区域的边缘和父元素的边缘之间。 Padding相比之下,它是位于元素内容区域内的缓冲区空间,位于元素的内容与元素的内部边缘之间。

XAML 属性用法

<object Margin="uniformThickness"/>  
- or -  
<object Margin="independentThickness"/>  
- or -  
<object Margin="qualifiedUniformThickness"/>  
- or -  
<object Margin="qualifiedIndependentThickness"/>  

XAML 值

uniformThickness
单个 Double 值的字符串表示形式,以统一应用于所有四个粗细尺寸。 例如,值 "10" 等效于值 "10,10,10,10"。 不限定的值以独立于设备的像素进行度量。 字符串不需要显式包含小数点。

independentThickness
按此顺序表示四个有序 Double 值,对应于左、上、右和下的独立粗细尺寸。 四个值必须用逗号分隔;不允许空格。 例如,“5,10,15,20”在内容左侧产生 5 像素的边距,内容上方的边距为 10 像素,内容右侧的边距为 15 像素,内容下方有 20 像素的边距。

qualifiedUniformThickness
由 uniformThickness 描述的值,后跟以下单位说明符之一: px``in

px (默认) 与设备无关的单位 (每单位 1/96 英寸)

in 为英寸;1in==96px

例如, "1in" 在所有方向上提供 1 英寸的统一边距。

qualifiedIndependentThickness
independentThickness 描述的值,每个独立值后跟以下单位说明符之一: px``in

px (默认) 与设备无关的单位 (每单位 1/96 英寸)

in 为英寸;1in==96px

例如,"1.5in,0.8in,1.5in,0.8in"。 单元说明符可以混合或省略一个或多个值。

依赖项属性信息

标识符字段 MarginProperty
元数据属性设置为 true AffectsMeasure

适用于