This is a migrated thread and some comments may be shown as answers.

How can I Striketrough a document by code

1 Answer 75 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Daní
Top achievements
Rank 1
Daní asked on 08 Mar 2011, 10:12 AM
Hello,

I'm showing a collection of text annotations using RichTextBox inside a ListBox. These annotation have an IsEnabled Property. When this property is set to true I'd like to Striketrough the text showed. As we are replacing an old system, many annotations come from data store in plain text, new ones, generated using a RadRichTextBox, I've created a custom DataProvider:

public class AnnotationDataProvider: DependencyObject
    {
        private XamlDataProvider _xamlProvider;
 
        private TxtDataProvider _txtProvider;
 
        public RadRichTextBox RichTextBox
        {
            get { return (RadRichTextBox)GetValue(RichTextBoxProperty); }
            set { SetValue(RichTextBoxProperty, value); }
        }
 
        // Using a DependencyProperty as the backing store for RichTextBox.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RichTextBoxProperty =
            DependencyProperty.Register("RichTextBox", typeof(RadRichTextBox), typeof(AnnotationDataProvider), new PropertyMetadata(OnUpdateRichTextBox));
 
 
 
        public AnnotationViewModel Annotation
        {
            get { return (AnnotationViewModel)GetValue(AnnotationProperty); }
            set { SetValue(AnnotationProperty, value); }
        }
 
        // Using a DependencyProperty as the backing store for Annotation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AnnotationProperty =
            DependencyProperty.Register("Annotation", typeof(AnnotationViewModel), typeof(AnnotationDataProvider), new PropertyMetadata(null, OnUpdateRichTextBox));
 
 
 
        private static void OnUpdateRichTextBox(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var provider = d as AnnotationDataProvider;
            provider.SetDataProvider();
        }
 
        private void SetDataProvider()
        {
            if (RichTextBox != null && Annotation != null)
                UpdateDocument();
        }
 
        void OnSetupDocument(object sender, SetupDocumentEventArgs e)
        {
            e.Document.LineSpacingType = Telerik.Windows.Documents.Model.LineSpacingType.Auto;
            e.Document.LineSpacing = 1;
            (sender as DataProviderBase).SetupDocument -= OnSetupDocument;
        }
 
        private void UpdateDocument()
        {
            if (Annotation.XamlText != ClinicalCourseExportConstants.EmptyXamlText)
                UpdateXaml();
            else
                UpdateTxt();
            if (Annotation.IsDisabled)
            {
                RichTextBox.Document.Selection.SelectAll();
                RichTextBox.ToggleStrikethrough();
                RichTextBox.Document.Selection.Clear();
            }
 
        }
        private void UpdateXaml()
        {
            if (_xamlProvider == null)
                _xamlProvider = new XamlDataProvider { RichTextBox = RichTextBox };
            _xamlProvider.SetupDocument += OnSetupDocument;
            _xamlProvider.Xaml = Annotation.XamlText;
            _xamlProvider.UpdateDocument();
        }
 
        private void UpdateTxt()
        {
            if (_txtProvider == null)
                _txtProvider = new TxtDataProvider { RichTextBox = RichTextBox };
            _txtProvider.SetupDocument += OnSetupDocument;
            _txtProvider.Text = Annotation.PlainText;
            _txtProvider.UpdateDocument();
        }

That works aparently fine, but using ToggleStrikethrough I'm not sure that text is Striketrough or not when needed. How can I, by code, ensure that all text in RichTextBox is Strikethrough?

1 Answer, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 12 Mar 2011, 08:20 AM
Hi Daní,

Please find attached a sample demo. Note that in the AnnotationDataProvider, you have to measure and arrange the document before being able to strike it through using RadRichTextBox's API.
I have included the second rich text box in the project, so that the structure of the document is clear at all times.
You can see how you can get if the current word/ whole document is stricken-through when pressing the buttons in the top right corner.
If that does not answer your question you need anything else, do not hesitate to contact us again.

Kind regards,
Iva
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
RichTextBox
Asked by
Daní
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Share this question
or