Programmatically add rows to a Table element.

image_pdfimage_print


   
  


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
    x:Class="WpfApplication1.Window1"
    Title="Add Content to a Table">
  <FlowDocumentScrollViewer HorizontalAlignment="Left" VerticalAlignment="Top">
    <FlowDocument>
      <Paragraph>
        <Button Canvas.Left="5" Click="AddRow">Add a New TableRow to the Table</Button>
      </Paragraph>

      <Table CellSpacing="5" Name="table1">
        <Table.Columns>
          <TableColumn/>
        </Table.Columns>
        <TableRowGroup Name="trg1">
          <TableRow>
            <TableCell>
              <Paragraph FontSize="14pt">TableRow</Paragraph>
            </TableCell>
          </TableRow>
        </TableRowGroup>
      </Table>
    </FlowDocument>
  </FlowDocumentScrollViewer>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace WpfApplication1
{

  public partial class Window1 : Window
  {
        public void AddRow(object sender, RoutedEventArgs e)
        {
            TableRow row = new TableRow();
            trg1.Rows.Add(row);
            Paragraph para = new Paragraph();
            para.Inlines.Add("A new Row and Cell have been Added to the Table");
            TableCell cell = new TableCell(para);
            row.Cells.Add(cell);
        }
    }
}

   
    
     


Table Flow Content

image_pdfimage_print


   
  
<Window x:Class="Documents.FlowContent"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="FlowContent" Height="400" Width="600">

    <FlowDocumentScrollViewer Name="docViewer" IsToolBarVisible="True" >
    <FlowDocument>      
      <BlockUIContainer>
        <Button Click="cmdCreateDynamicDocument_Click" HorizontalAlignment="Left" Padding="5">Create a Dynamic Document</Button>
      </BlockUIContainer>
      <Paragraph FontSize="20pt">Largest Cities in the Year 100</Paragraph>
      <Table>
        <Table.Columns>
          <TableColumn Width="*"></TableColumn>
          <TableColumn Width="3*"></TableColumn>
          <TableColumn Width="*"></TableColumn>
        </Table.Columns>

        <TableRowGroup  >
          <TableRow FontWeight="Bold" >
            <TableCell >
              <Paragraph>Title 1</Paragraph>
            </TableCell>
            <TableCell>
              <Paragraph>Title 2</Paragraph>
            </TableCell>
            <TableCell>
              <Paragraph>Title 3</Paragraph>
            </TableCell>
          </TableRow>
          <TableRow>
            <TableCell>
              <Paragraph>A</Paragraph>
            </TableCell>
            <TableCell>
              <Paragraph>A</Paragraph>
            </TableCell>
            <TableCell>
              <Paragraph>A</Paragraph>
            </TableCell>
          </TableRow>
        </TableRowGroup>
      </Table>
    </FlowDocument>
  </FlowDocumentScrollViewer>

</Window>

//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Documents
{
    public partial class FlowContent : System.Windows.Window
    {
        public FlowContent()
        {
            InitializeComponent();           
        }
        private void cmdCreateDynamicDocument_Click(object sender, RoutedEventArgs e)
        {
            Run runFirst = new Run();
            runFirst.Text = "A ";

            Bold bold = new Bold();
            Run runBold = new Run();
            runBold.Text = "bold ";
            bold.Inlines.Add(runBold);

            Run runLast = new Run();
            runLast.Text = " documents";

            Paragraph paragraph = new Paragraph();
            paragraph.Inlines.Add(runFirst);
            paragraph.Inlines.Add(bold);
            paragraph.Inlines.Add(runLast);

            FlowDocument document = new FlowDocument();
            document.Blocks.Add(paragraph);

            docViewer.Document = document;
        }
    }
}

   
    
     


Annotation Service

image_pdfimage_print


   
  

<Window   
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:a="clr-namespace:System.Windows.Annotations;assembly=PresentationFramework"
    Title="Flow Document Reader + Annotations"
  x:Class="Window1" Initialized="OnInitialized" Closed="OnClosed">
  <StackPanel>
    <StackPanel Orientation="Horizontal">
      <Label>Control Annotations:</Label>
      <Button Command="a:AnnotationService.CreateTextStickyNoteCommand"
         CommandTarget="{Binding ElementName=reader}">Create Text Note</Button>
      <Button Command="a:AnnotationService.CreateInkStickyNoteCommand" CommandTarget="{Binding ElementName=reader}">
        Create Ink Note
      </Button>
      <Button Command="a:AnnotationService.DeleteStickyNotesCommand" CommandTarget="{Binding ElementName=reader}">
        Remove Note
      </Button>
      <Button Command="a:AnnotationService.CreateHighlightCommand"  CommandTarget="{Binding ElementName=reader}">
        Create Yellow Highlight
      </Button>
      <Button Command="a:AnnotationService.ClearHighlightsCommand" CommandTarget="{Binding ElementName=reader}">
        Remove Highlight
      </Button>
    </StackPanel>

    <FlowDocumentReader x:Name="reader">
      <FlowDocument>
        <Paragraph FontSize="22" FontWeight="Bold">Chapter 1</Paragraph>
        <Paragraph FontSize="35" FontWeight="Bold">Why WPF?</Paragraph>
        <Paragraph>
          this is a test
          this is a test
          this is a test
          this is a test
          this is a test
          this is a test
          this is a test
        </Paragraph>
        <Paragraph>
                this is a test 
                this is a test 
                this is a test 
                this is a test 
                this is a test 
                this is a test 
                this is a test 
                this is a test 
                
        </Paragraph>
        <Paragraph>
          this is another test
        </Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
        <Paragraph>...</Paragraph>
      </FlowDocument>
    </FlowDocumentReader>
  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.IO;
using System.Windows;
using System.Windows.Annotations;
using System.Windows.Annotations.Storage;

public partial class Window1 : Window
{
    Stream stream;

    public Window1()
    {
        InitializeComponent();
    }

    protected void OnInitialized(object sender, EventArgs e)
    {
        AnnotationService service = AnnotationService.GetService(reader);
        if (service == null)
        {
            stream = new FileStream("storage.xml", FileMode.OpenOrCreate);
            service = new AnnotationService(reader);
            AnnotationStore store = new XmlStreamStore(stream);
            service.Enable(store);
        }
    }

    protected void OnClosed(object sender, EventArgs e)
    {
        AnnotationService service = AnnotationService.GetService(reader);
        if (service != null &amp;&amp; service.IsEnabled)
        {
            service.Store.Flush();
            service.Disable();
            stream.Close();
        }
    }
}

   
    
     


Toggle Hyphenation, Optimal Paragraph, and Column Flex

image_pdfimage_print


   
  


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="FlowDocument Optimal Paragraph Sample">
  <StackPanel>
        <CheckBox Click="ToggleHyphenation" Content="Automatic Hyphenation"/>
        <CheckBox Click="ToggleOptimalParagraph" Content="  Enable Optimal Paragraph Layout"/>
        <CheckBox Click="ToggleColumnFlex" Content="Enable Flexible Columns"/>
        <Slider Name="columnWidthSlider" ValueChanged="ChangeColumnWidth"/>
        <Slider Name="columnGapSlider" ValueChanged="ChangeColumnGap"/>
    <FlowDocumentReader Name="flowReader">
      <FlowDocument Name="flowDoc"        TextAlignment="Justify" 
        IsOptimalParagraphEnabled="True"  IsHyphenationEnabled="True"
        IsColumnWidthFlexible="True"      Background="AliceBlue"
        ColumnWidth="300"                 ColumnGap="20">
        <Paragraph><Italic>this is a test</Italic></Paragraph>
        <Paragraph>
          <Hyperlink NavigateUri="http://www-cs-faculty.stanford.edu/~knuth/">
            - Donald E. Knuth
          </Hyperlink>
        </Paragraph>
        <Paragraph><Bold>Principle of Optimal Paragraph</Bold></Paragraph>
        <Paragraph>this is a test <Italic>italic</Italic> </Paragraph>
        <Paragraph>this is a test</Paragraph>
        <Paragraph>this is a test</Paragraph>
        <Paragraph>this is a test</Paragraph>
      </FlowDocument>
    </FlowDocumentReader>
  </StackPanel>
</Window>


//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Shapes;


namespace WpfApplication1
{
    public partial class Window1 : Window
    {

        void ToggleHyphenation(Object sender, RoutedEventArgs args)
        {
            flowDoc.IsHyphenationEnabled = ((CheckBox)sender).IsChecked.Value;
        }

        void ToggleOptimalParagraph(Object sender, RoutedEventArgs args)
        {
            flowDoc.IsOptimalParagraphEnabled = ((CheckBox)sender).IsChecked.Value;            
        }

        void ToggleColumnFlex(Object sender, RoutedEventArgs args)
        {
            flowDoc.IsColumnWidthFlexible = ((CheckBox)sender).IsChecked.Value;
        }

        void ChangeColumnWidth(Object sender, RoutedEventArgs args)
        {
                if (columnWidthSlider.Value == 0) 
                {
                    flowDoc.ColumnWidth = 100;
                }
                else if (columnWidthSlider.Value == 1)
                {
                    flowDoc.ColumnWidth = 200;
                }
                else if (columnWidthSlider.Value == 2)
                {
                    flowDoc.ColumnWidth = 300;
                }
                else if (columnWidthSlider.Value == 3)
                { 
                    flowDoc.ColumnWidth = 400; 
                }
                else if (columnWidthSlider.Value == 4)
                { 
                    flowDoc.ColumnWidth = 500; 
                }
        }
        void ChangeColumnGap(Object sender, RoutedEventArgs args)
        {
                if (columnGapSlider.Value == 0)
                {
                    flowDoc.ColumnGap = 5;
                }
                else if (columnGapSlider.Value == 1)
                {
                    flowDoc.ColumnGap = 10;
                }
                else if (columnGapSlider.Value == 2)
                {
                    flowDoc.ColumnGap = 15;
                }
                else if (columnGapSlider.Value == 3)
                {
                    flowDoc.ColumnGap = 20;
                }
                else if (columnGapSlider.Value == 4)
                {
                    flowDoc.ColumnGap = 25;
                }
        }
    }
}

   
    
     


Change font

image_pdfimage_print


   
  

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="Typography_Samp.Page1"
  WindowTitle="Typography Variants Sample">
  <FlowDocument Name="tf1" FontSize="18" ColumnWidth="600.0">
    <Paragraph FontSize="20" Margin="0,0,0,10">Typography Variants Sample</Paragraph>

    <Paragraph Margin="0,0,0,50">
      this is a test
    </Paragraph>

    <Paragraph>
      <StackPanel Orientation="Horizontal" Margin="10" HorizontalAlignment="Center">
        <Button Click="changeArial">Arial</Button>
        <Button Click="changePalatino">Palatino Linotype</Button>
        <Button Click="changeTimes">Times New Roman</Button>
        <Button Click="changeVerdana">Verdana</Button>
      </StackPanel>

      <LineBreak/>
      Normal <Run Typography.Variants="Superscript">superscript</Run>
      <Run Typography.Variants="Subscript">subscript</Run>
      1<Run Typography.Variants="Ordinal">st</Run>
      H<Run Typography.Variants="Inferior">2</Run>
      SO<Run Typography.Variants="Inferior">4</Run>
    </Paragraph>
    
  </FlowDocument>
</Page>

//File:Window.xaml.cs

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

namespace Typography_Samp
{

  public partial class Page1 : Page
  {
        public void changeArial(object sender, RoutedEventArgs e)
        {
            tf1.FontFamily = new FontFamily("Arial");
        }

        public void changePalatino(object sender, RoutedEventArgs e)
        {
            tf1.FontFamily = new FontFamily("Palatino Linotype");
        }

        public void changeTimes(object sender, RoutedEventArgs e)
        {
            tf1.FontFamily = new FontFamily("Times New Roman");
        }

        public void changeVerdana(object sender, RoutedEventArgs e)
        {
            tf1.FontFamily = new FontFamily("Verdana");
        }
    }
}

   
    
     


Fire an event when an element gains and loses focus.

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom="clr-namespace:WpfApplication1"  
    Title="gotfocusLostfocusEffectUsingEvent" Height="400" Width="450"
    >
  <StackPanel>
    <StackPanel.Resources>
      <Style TargetType="{x:Type Button}">
        <Setter Property="Height" Value="20"/>
        <Setter Property="Width" Value="250"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
      </Style>
    </StackPanel.Resources>
    <Button
        GotFocus="OnGotFocusHandler"
        LostFocus="OnLostFocusHandler">Click Or Tab To Give Keyboard Focus</Button>
    <Button
        GotFocus="OnGotFocusHandler"
        LostFocus="OnLostFocusHandler">Click Or Tab To Give Keyborad Focus</Button>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Input;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void OnGotFocusHandler(object sender, RoutedEventArgs e)
        {
            Button tb = e.Source as Button;
            tb.Background = Brushes.Red;
        }
        private void OnLostFocusHandler(object sender, RoutedEventArgs e)
        {
            Button tb = e.Source as Button;
            tb.Background = Brushes.White;
        }
    }
}

   
    
     


Font Properties Moved

image_pdfimage_print


   
      

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  Title="About WPF" SizeToContent="WidthAndHeight"
  Background="OrangeRed">
  <StackPanel>
    <Label FontWeight="Bold" FontSize="20" Foreground="White">
      WPF Version
    </Label>
    <Label>2010</Label>
    <Label>Installed Dll:</Label>
    <ListBox>
      <ListBoxItem>1</ListBoxItem>
      <ListBoxItem>2</ListBoxItem>
    </ListBox>
    <StackPanel TextElement.FontSize="30" TextElement.FontStyle="Italic"
      Orientation="Horizontal" HorizontalAlignment="Center">
      <Button MinWidth="75" Margin="10">Help</Button>
      <Button MinWidth="75" Margin="10">OK</Button>
    </StackPanel>
    <StatusBar>You have successfully registered this product.</StatusBar>
  </StackPanel>
</Window>