XAML Section

This article shows how to create and use a Section in a WPF FlowDocument using XAML.
  • 2694

Section is used only to contain other Block-derived elements. It does not apply any default formatting to the elements it contains. However, any property values set on a Section applies to its child elements. A section also enables you to programmatically iterate through its child collection.
Section  is used in a similar manner to the <DIV> tag in HTML.
In the example below, three paragraphs are defined under one Section . The section has a Background property value of Red, therefore the background color of the paragraphs is also red.

Here is XAML example:
 

<FlowDocument >

    <Section Background="Red">

        <Paragraph>

            Paragraph 1

        </Paragraph>

        <Paragraph>

            Paragraph 2

        </Paragraph>

        <Paragraph>

            Paragraph 3

        </Paragraph>

    </Section>

</FlowDocument>
 


C# example:
 


using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Documents;

namespace SDKSample
{
    public partial class SectionExample : Page
    {
        public SectionExample()
        {

            // Create three paragraphs
            Paragraph myParagraph1 = new Paragraph(new Run("Paragraph 1"));
            Paragraph myParagraph2 = new Paragraph(new Run("Paragraph 2"));
            Paragraph myParagraph3 = new Paragraph(new Run("Paragraph 3"));

            // Create a Section and add the three paragraphs to it.
            Section mySection = new Section();
            mySection.Background = Brushes.Red;

            mySection.Blocks.Add(myParagraph1);
            mySection.Blocks.Add(myParagraph2);
            mySection.Blocks.Add(myParagraph3);

            // Create a FlowDocument and add the section to it.
            FlowDocument myFlowDocument = new FlowDocument();
            myFlowDocument.Blocks.Add(mySection);

            this.Content = myFlowDocument;
        }
    }
}

 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

 

© 2019 DotNetHeaven. All rights reserved.