Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to show table data using print priview in text file..
Posted
Comments
Kornfeld Eliyahu Peter 10-Feb-14 3:13am    
Have you done anything so far? Show some effort (code or searching)! As is it ain't a question...

1 solution

Hi

Using below user control you can bind datasource with richtext box

RichTextControl.xaml

<usercontrol x:class="Controls.RichTextControl" xmlns:x="#unknown">
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <grid>
        <richtextbox x:name="RTextBox" verticalscrollbarvisibility="Auto" />
    </grid>
</usercontrol>


RichTextControl.xaml.cs

public partial class RichTextControl : UserControl
    {
        public RichTextControl()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty DocumentProperty =
           DependencyProperty.Register("Document", typeof(FlowDocument),
           typeof(RichTextControl), new PropertyMetadata(OnDocumentChanged));

        public FlowDocument Document
        {
            get { return (FlowDocument)GetValue(DocumentProperty); }
            set { SetValue(DocumentProperty, value); }
        }
        private bool _IsReadOnly = false;
        public bool IsReadOnly
        {
            get
            {
                return RTextBox.IsReadOnly;
            }
            set 
            {
                _IsReadOnly = value;
                RTextBox.IsReadOnly = _IsReadOnly;
            }
        }

        private static void OnDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var thisControl = (RichTextControl)d;
            thisControl.RTextBox.Document = (e.NewValue == null) ? new FlowDocument() : (FlowDocument)e.NewValue;
        }

        public void UpdateDocumentBindings()
        {
            SetValue(DocumentProperty, this.RTextBox.Document);
        }
    }


RichTextConverter.cs

C#
public class RichTextConverter : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var flowDocument = new FlowDocument();
            if (value != null)
            {
                var xamlText = (string)value;
                flowDocument = (FlowDocument)XamlReader.Parse(xamlText);
            }
            return flowDocument;
        }
        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return string.Empty;
            var flowDocument = (FlowDocument)value;
            return XamlWriter.Save(flowDocument);
        }
    }


Use below to set binding

<control:richtextcontrol x:name="RTMail" document="{Binding Path=Message, Converter={StaticResource RichTextConverter}, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True,NotifyOnValidationError=True}" xmlns:x="#unknown" xmlns:control="#unknown"></control:richtextcontrol>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900