DXRichEdit for WPF - How to implement progress indicator for slow RichEditControl operations
This example demonstrates how to utilize the IProgressIndicationService service interface of the RichEditControl. The ProgressBar control on the form indicates how the mail merge operation proceeds.
Files to Review
- MainWindow.xaml (VB: MainWindow.xaml)
- MainWindow.xaml.cs (VB: MainWindow.xaml)
- MyProgressIndicator.cs (VB: MyProgressIndicator.vb)
- SampleData.cs (VB: SampleData.vb)
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
This example demonstrates how to utilize the IProgressIndicationService service interface of the RichEditControl. The ProgressBar control on the form indicates how the mail merge operation proceeds.
Files to Review
- MainWindow.xaml (VB: MainWindow.xaml)
- MainWindow.xaml.cs (VB: MainWindow.xaml)
- MyProgressIndicator.cs (VB: MyProgressIndicator.vb)
- SampleData.cs (VB: SampleData.vb)
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
XAML<Window x:Class="ProgressIndicator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxre="http://schemas.devexpress.com/winfx/2008/xaml/richedit"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns:dxr="http://schemas.devexpress.com/winfx/2008/xaml/ribbon"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
Title="MainWindow" Height="600" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Button Name="btnMailMerge" Content="Mail Merge" Width="200" HorizontalAlignment="Left" Margin="5" Click="btnMailMerge_Click" />
<dx:DXTabControl Name="tabControl" Grid.Row="1" SelectionChanged="tabControl_SelectionChanged">
<dx:DXTabItem Header="Template">
<dxre:RichEditControl Name="richEditControl1" Loaded="richEditControl1_Loaded" MailMergeStarted="richEditControl1_MailMergeStarted" MailMergeFinished="richEditControl1_MailMergeFinished" MailMergeRecordStarted="richEditControl1_MailMergeRecordStarted" MailMergeRecordFinished="richEditControl1_MailMergeRecordFinished" />
</dx:DXTabItem>
<dx:DXTabItem Header="Result">
<dxre:RichEditControl Name="richEditControl2" />
</dx:DXTabItem>
</dx:DXTabControl>
<dxe:ProgressBarEdit Name="progressBarControl1" Grid.Row="2" Visibility="Collapsed" />
</Grid>
</Window>
C#using System;
using System.Windows;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit;
#region #usings
using DevExpress.Services;
#endregion #usings
namespace ProgressIndicator {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void richEditControl1_Loaded(object sender, RoutedEventArgs e) {
richEditControl1.ApplyTemplate();
richEditControl1.LoadDocument("Docs\\invitation.docx");
richEditControl1.Options.MailMerge.DataSource = new SampleData();
}
private void btnMailMerge_Click(object sender, RoutedEventArgs e) {
MailMergeOptions myMergeOptions = richEditControl1.Document.CreateMailMergeOptions();
myMergeOptions.MergeMode = MergeMode.NewSection;
richEditControl1.Document.MailMerge(myMergeOptions, richEditControl2.Document);
tabControl.SelectedIndex = 1;
}
private void richEditControl1_MailMergeStarted(object sender, MailMergeStartedEventArgs e) {
#region #servicesubst
richEditControl1.ReplaceService<IProgressIndicationService>
(new MyProgressIndicatorService(richEditControl1, this.progressBarControl1));
#endregion #servicesubst
}
private void richEditControl1_MailMergeFinished(object sender, MailMergeFinishedEventArgs e) {
richEditControl1.RemoveService(typeof(IProgressIndicationService));
}
private void richEditControl1_MailMergeRecordStarted(object sender, MailMergeRecordStartedEventArgs e) {
// Imitating slow data fetching
System.Threading.Thread.Sleep(100);
}
private void richEditControl1_MailMergeRecordFinished(object sender, MailMergeRecordFinishedEventArgs e) {
e.RecordDocument.AppendDocumentContent("Docs\\bungalow.docx", DocumentFormat.OpenXml);
}
private void tabControl_SelectionChanged(object sender, DevExpress.Xpf.Core.TabControlSelectionChangedEventArgs e) {
switch (tabControl.SelectedIndex) {
case 0:
this.btnMailMerge.IsEnabled = true;
break;
case 1:
this.btnMailMerge.IsEnabled = false;
break;
}
}
}
}
C#// Developer Express Code Central Example:
// How to implement progress indicator for slow RichEditControl operations
//
// This example demonstrates how to utilize the IProgressIndicationService service
// interface of the RichEditControl. The ProgressBar control on the form indicates
// how the mail merge operation proceeds.
//
// You can find sample updates and versions for different programming languages here:
// http://www.devexpress.com/example=E3075
using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.Services;
using System.Windows.Forms;
using DevExpress.Xpf.Editors;
using System.Windows.Threading;
namespace ProgressIndicator
{
#region #myprogressindicator
class MyProgressIndicatorService : IProgressIndicationService
{
private ProgressBarEdit _Indicator;
public ProgressBarEdit Indicator
{
get { return _Indicator; }
set { _Indicator = value; }
}
public MyProgressIndicatorService(IServiceProvider provider, ProgressBarEdit indicator)
{
_Indicator = indicator;
}
#region IProgressIndicationService Members
void IProgressIndicationService.Begin(string displayName, int minProgress, int maxProgress, int currentProgress)
{
_Indicator.Minimum = minProgress;
_Indicator.Maximum = maxProgress;
_Indicator.EditValue = currentProgress;
_Indicator.Visibility = System.Windows.Visibility.Visible;
Refresh();
}
void IProgressIndicationService.End()
{
_Indicator.Visibility = System.Windows.Visibility.Collapsed;
Refresh();
}
void IProgressIndicationService.SetProgress(int currentProgress)
{
_Indicator.EditValue = currentProgress;
Refresh();
}
#endregion
void Refresh() {
Action emptyDelegate = delegate() { };
_Indicator.Dispatcher.Invoke(DispatcherPriority.Render, emptyDelegate);
}
}
#endregion #myprogressindicator
}
C#// Developer Express Code Central Example:
// How to implement progress indicator for slow RichEditControl operations
//
// This example demonstrates how to utilize the IProgressIndicationService service
// interface of the RichEditControl. The ProgressBar control on the form indicates
// how the mail merge operation proceeds.
//
// You can find sample updates and versions for different programming languages here:
// http://www.devexpress.com/example=E3075
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ProgressIndicator {
class SampleData : ArrayList {
public SampleData() {
Add(new AddresseeRecord("Maria", "Alfreds Futterkiste", "Obere Str. 57, Berlin"));
Add(new AddresseeRecord("Ana", "Ana Trujillo Emparedados y helados", "Avda. de la Constitucion 2222, Mexico D.F."));
Add(new AddresseeRecord("Antonio", "Antonio Moreno Taqueria", "Mataderos 2312, Mexico D.F."));
Add(new AddresseeRecord("Thomas", "Around the Horn", "120 Hanover Sq., London"));
Add(new AddresseeRecord("Christina", "Berglunds snabbkop", "Berguvsvagen 8, Lulea"));
Add(new AddresseeRecord("Hanna", "Blauer See Delikatessen", "Forsterstr. 57, Mannheim"));
Add(new AddresseeRecord("Frederique", "Blondel pere et fils", "24, place Kleber, Strasbourg"));
Add(new AddresseeRecord("Martin", "Bolido Comidas preparadas", "C/ Araquil, 67, Madrid"));
Add(new AddresseeRecord("Laurence", "Bon app'", "12, rue des Bouchers, Marseille"));
Add(new AddresseeRecord("Elizabeth", "Bottom-Dollar Markets", "23 Tsawassen Blvd., Tsawwassen"));
Add(new AddresseeRecord("Victoria", "B's Beverages", "Fauntleroy Circus, London"));
Add(new AddresseeRecord("Patricio", "Cactus Comidas para llevar", "Cerrito 333, Buenos Aires"));
Add(new AddresseeRecord("Francisco", "Centro comercial Moctezuma", "Sierras de Granada 9993, Mexico D.F."));
Add(new AddresseeRecord("Yang", "Chop-suey Chinese", "Hauptstr. 29, Bern"));
Add(new AddresseeRecord("Pedro", "Comercio Mineiro", "Av. dos Lusiadas, 23, Sao Paulo"));
Add(new AddresseeRecord("Elizabeth","Consolidated Holdings","Berkeley Gardens12 Brewery , London"));
Add(new AddresseeRecord("Sven","Drachenblut Delikatessen","Walserweg 21, Aachen"));
Add(new AddresseeRecord("Janine", "Du monde entier", "67, rue des Cinquante Otages, Nantes"));
}
}
public class AddresseeRecord {
private string fName;
private string fCompany;
private string fAddress;
public string Name {
get { return fName; }
set { fName = value; }
}
public string Company {
get { return fCompany; }
set { fCompany = value; }
}
public string Address {
get { return fAddress; }
set { fAddress = value; }
}
public AddresseeRecord(string fName, string fCompany, string fAddress) {
this.fName = fName;
this.fCompany = fCompany;
this.fAddress = fAddress;
}
}
}