Επεξεργασία

Κοινή χρήση μέσω


Block.Margin Property

Definition

Gets or sets the margin thickness for the element.

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

Property Value

A Thickness structure that specifies the amount of margin to apply, in device independent pixels. The default is a uniform thickness of zero (0.0).

Examples

The following example shows how to set the Margin attribute of a Block element (Paragraph).

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

The following figure shows how the preceding example renders. Exaggerated thicknesses and colors are used for illustration.

Screenshot: Paragraphs with padding and margins

The following example shows how to set the Margin property programmatically.

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)

Remarks

Margin is buffer space that falls outside an element's content area, between the edges of the element's content area and the edges of the parent element. Contrast with Padding, which is buffer space that falls inside an element's content area, between the element's content and the inner edge of the element.

XAML Attribute Usage

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

XAML Values

uniformThickness
String representation of a single Double value to apply uniformly to all four thickness dimensions. For example, a value of "10" is equivalent to a value of "10,10,10,10". An unqualified value is measured in device independent pixels. Strings need not explicitly include decimal points.

independentThickness
String representation of four ordered Double values corresponding to independent thickness dimensions for left, top, right, and bottom, in this order. The four values must be separated with commas; spaces are not allowed. For example, "5,10,15,20" results in 5 pixels of margin to the left of content, 10 pixels of margin above content, 15 pixels of margin to the right of content, and 20 pixels of margin below the content.

qualifiedUniformThickness
A value described by uniformThickness followed by one of the following unit specifiers: px, in.

px (default) is device-independent units (1/96th inch per unit)

in is inches; 1in==96px

For example, "1in" provides uniform margin of 1 inch in all directions.

qualifiedIndependentThickness
A value described by independentThickness, with each independent value followed by one of the following unit specifiers: px, in.

px (default) is device-independent units (1/96th inch per unit)

in is inches; 1in==96px

For example, "1.5in,0.8in,1.5in,0.8in". Unit specifiers may be mixed or omitted from one or more values.

Dependency Property Information

Identifier field MarginProperty
Metadata properties set to true AffectsMeasure

Applies to

See also