Search This Blog

Thursday, November 21, 2013

Example: Load and save content of WPF RichTextBox to a file

WPF > Controls  > ContentControl > RichTextBox

RichTextBox operates on FlowDocument objects.

Note: Pasting HTML content into a RichTextBox might result in unexpected behavior because RichTextBox uses RTF format rather than directly using HTML format.

Examples


Example

Load and save content of WPF RichTextBox to a file


XAML:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:src="clr-namespace:WpfApplication2"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication2.MainWindow"
        Title="MainWindow"
       d:DesignWidth="300" d:DesignHeight="320"
        >
    <Grid HorizontalAlignment="Left" Height="295" VerticalAlignment="Top" Width="300">
        <Button x:Name="btnSave" Content="Save" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="btnSave_Click"/>
        <Button x:Name="btnLoad" Content="Load" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="80,0,0,0" Click="btnLoad_Click"/>
        <RichTextBox x:Name="rtb" HorizontalAlignment="Left" Height="267" Margin="0,27,0,0" VerticalAlignment="Top" Width="300">
            <FlowDocument>
                <Paragraph>
                    <Run Text=""/>
                </Paragraph>
            </FlowDocument>
        </RichTextBox>
    </Grid>
</Window>

C# Code

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApplication2
{
     /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    ///

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            TextRange t = new TextRange(rtb.Document.ContentStart,rtb.Document.ContentEnd);
            FileStream file = new FileStream("c:\\rtb.xaml", FileMode.Create);
            t.Save(file, System.Windows.DataFormats.XamlPackage);
             file.Close();
        }
        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            TextRange t = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
            FileStream file = new FileStream("c:\\rtb.xaml", FileMode.Open);
            t.Load(file, System.Windows.DataFormats.XamlPackage);
            file.Close();
        }
    }
}

Tuesday, November 19, 2013

FlowDocumentPageViewer WPF Example

WPF > Documents > FlowDocumentPageViewer

FlowDocumentPageViewer shows content in a page at a time in view mode.  It is fixed to a particular viewing mode.

Components:
  • Content Area
  • Toolbar
  • Page Navigation Controls
  • Zoom Controls
  • Search - The user can use the Ctr+F keyboard shortcut
Example:


<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:NumericTextBox="clr-namespace:NumericTextBox;assembly=NumericTextBox" x:Class="WpfApplication3.MainWindow"
        Title="MainWindow" Height="400" Width="600">
    <FlowDocumentPageViewer Margin="10" BorderBrush="Black" BorderThickness="1">
        <FlowDocument ColumnWidth="400" IsOptimalParagraphEnabled="True" IsHyphenationEnabled="True">
            <Section FontSize="12">
                <Paragraph>
                    <Bold>Paragraph bold</Bold>
                   fdgfdgfdsggggggggggggg  sgflkklgf  gf s fgiohiohiofhds gfd gfgoihiw4hrior3    fgreretretretre rrtreretertrererererererererere
                    rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
                    tttttttttttttttttttttttttttttttttttttyyyyyyyyyyyyyy
                    ytuyutyutyuytuytuuuuuuuuuuuuuuuuuuuuuuuu
                    uytuyuytutyutyutyutyuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
                    <Figure Width="140" Height="50" Background="GhostWhite" HorizontalAnchor="PageLeft" HorizontalOffset="300" VerticalOffset="20">
                        <Paragraph FontStyle="Italic" TextAlignment="Left" Background="Beige" Foreground="DarkGreen">
                           Figure is here
                        </Paragraph>
                    </Figure>
                </Paragraph>
             </Section>
            <Section>
                <Paragraph>Sales
                <Floater    HorizontalAlignment="Left">
                    <Table>
                    <Table.Resources>
                        <!-- Style for header/footer rows. -->
                        <Style x:Key="headerFooterRowStyle" TargetType="{x:Type TableRowGroup}">
                            <Setter Property="FontWeight" Value="DemiBold"/>
                            <Setter Property="FontSize" Value="16"/>
                            <Setter Property="Background" Value="LightGray"/>
                        </Style>
                        <!-- Style for data rows. -->
                        <Style x:Key="dataRowStyle" TargetType="{x:Type TableRowGroup}">
                            <Setter Property="FontSize" Value="12"/>
                            <Setter Property="FontStyle" Value="Italic"/>
                        </Style>
                    </Table.Resources>
                    <Table.Columns>
                        <TableColumn/>
                        <TableColumn/>
                        <TableColumn/>
                        <TableColumn/>
                    </Table.Columns>
                    <!-- This TableRowGroup hosts a header row for the table. -->
                    <TableRowGroup Style="{StaticResource headerFooterRowStyle}">
                        <TableRow>
                            <TableCell/>
                            <TableCell>
                                <Paragraph>Puma</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>Adidas</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>Nike</Paragraph>
                            </TableCell>
                        </TableRow>
                    </TableRowGroup>
                    <!-- This TableRowGroup hosts the main data rows for the table. -->
                    <TableRowGroup Style="{StaticResource dataRowStyle}">
                        <TableRow>
                          <TableCell>
                                <Paragraph Foreground="Blue">Blue</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>1</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>2</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>3</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell>
                                <Paragraph Foreground="Red">Red</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>1</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>2</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>3</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell>
                                <Paragraph Foreground="Green">Green</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>1</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>2</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>3</Paragraph>
                            </TableCell>
                        </TableRow>
                    </TableRowGroup>
                   <!-- This TableRowGroup hosts a footer row for the table. -->
                    <TableRowGroup Style="{StaticResource headerFooterRowStyle}">
                        <TableRow>
                            <TableCell>
                                <Paragraph>Total</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>3</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>6</Paragraph>
                            </TableCell>
                            <TableCell>
                                        <Paragraph>12</Paragraph>
                                    </TableCell>
                        </TableRow>
                    </TableRowGroup>
                </Table>
</Floater>
                </Paragraph>
            </Section>
        </FlowDocument>
    </FlowDocumentPageViewer>
</Window>