Get text from RichTextBox without selection

8 posts, 1 answers
  1. Ivan
    Ivan avatar
    43 posts
    Member since:
    Nov 2013

    Posted 14 Feb 2014 Link to this post

    Hello,

    i tried to get the text in RichTextBox with Binding with a property of my ViewModel with type 'string'.
    But
    something with binding is always wrong. I couldn't pass a defined
    string from ViewModel, and i got always string.empty from RichTextBox.

    Here is my code in xaml, i also tried RadWatermarkTextBox, that works fine.
    <telerik:XamlDataProvider RichTextBox="{Binding ElementName=richTextBox1}" Xaml="{Binding PropertyName, Mode=TwoWay}" />
    <telerik:RadRichTextBox Name="radRichTextBox"/>
     
    <telerik:RadWatermarkTextBox Text="{Binding Path=PropertyName, Mode=TwoWay}"/>

    I have seen some articals with using selection. But i don't want to use it instead of lovely binding...
    Another try i have done is with TextRange
    var startPosition = new DocumentPosition(radRichTextBox.Document.CaretPosition);
    var endPosition = new DocumentPosition(radRichTextBox.Document.CaretPosition);
    startPosition.MoveToFirstPositionInDocument();
    endPosition.MoveToLastPositionInDocument();
     
    var text = new TextRange(
               // TextPointer to the start of content in the RichTextBox.
               startPosition,
               // TextPointer to the end of content in the RichTextBox.
               endPosition).Text;
    But Telerik TextRange has difference of MS TextRange, it has no property Text...

    Is there a easy way to implement that? That could not be so hard, right? Or what i did wrong in my code?
  2. Petya
    Admin
    Petya avatar
    975 posts

    Posted 19 Feb 2014 Link to this post

    Hi,

    Please refer to this SDK example where data binding is demonstrated. Note that if you want to set the value of the XamlData property in order for the set string to show in RadRichTextBox, you need to make sure the string is in XAML format, i.e..
    <t:RadDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:t="clr-namespace:Telerik.Windows.Documents.Model;assembly=Telerik.Windows.Documents" xmlns:s="clr-namespace:Telerik.Windows.Documents.Model.Styles;assembly=Telerik.Windows.Documents" xmlns:r="clr-namespace:Telerik.Windows.Documents.Model.Revisions;assembly=Telerik.Windows.Documents" xmlns:n="clr-namespace:Telerik.Windows.Documents.Model.Notes;assembly=Telerik.Windows.Documents" xmlns:th="clr-namespace:Telerik.Windows.Documents.Model.Themes;assembly=Telerik.Windows.Documents" version="1.2" LayoutMode="Paged" LineSpacing="1.15" LineSpacingType="Auto" ParagraphDefaultSpacingAfter="12" ParagraphDefaultSpacingBefore="0" SelectedBibliographicStyleName="\APA.XSL" StyleName="defaultDocumentStyle">
      <t:RadDocument.Captions>
        <t:CaptionDefinition IsDefault="True" IsLinkedToHeading="False" Label="Figure" LinkedHeadingLevel="0" NumberingFormat="Arabic" SeparatorType="Hyphen" />
        <t:CaptionDefinition IsDefault="True" IsLinkedToHeading="False" Label="Table" LinkedHeadingLevel="0" NumberingFormat="Arabic" SeparatorType="Hyphen" />
      </t:RadDocument.Captions>
      <t:RadDocument.ProtectionSettings>
        <t:DocumentProtectionSettings EnableDocumentProtection="False" Enforce="False" HashingAlgorithm="None" HashingSpinCount="0" ProtectionMode="ReadOnly" />
      </t:RadDocument.ProtectionSettings>
      <t:RadDocument.Styles>
        <s:StyleDefinition DisplayName="Document Default Style" IsCustom="False" IsDefault="False" IsPrimary="True" Name="defaultDocumentStyle" Type="Default">
          <s:StyleDefinition.ParagraphStyle>
            <s:ParagraphProperties LineSpacing="1.15" SpacingAfter="12" />
          </s:StyleDefinition.ParagraphStyle>
          <s:StyleDefinition.SpanStyle>
            <s:SpanProperties FontFamily="Verdana" FontSize="16" FontStyle="Normal" FontWeight="Normal" />
          </s:StyleDefinition.SpanStyle>
        </s:StyleDefinition>
        <s:StyleDefinition DisplayName="Normal" IsCustom="False" IsDefault="True" IsPrimary="True" Name="Normal" Type="Paragraph" UIPriority="0" />
        <s:StyleDefinition DisplayName="Table Normal" IsCustom="False" IsDefault="True" IsPrimary="False" Name="TableNormal" Type="Table" UIPriority="59">
          <s:StyleDefinition.TableStyle>
            <s:TableProperties CellPadding="5,0,5,0">
              <s:TableProperties.TableLook>
                <t:TableLook />
              </s:TableProperties.TableLook>
            </s:TableProperties>
          </s:StyleDefinition.TableStyle>
        </s:StyleDefinition>
      </t:RadDocument.Styles>
      <t:Section>
        <t:Paragraph>
          <t:Span Text="test document" />
        </t:Paragraph>
      </t:Section>
    </t:RadDocument>
      
    If your data is plain text you could replace the XamlDataProvider with TxtDataProvider and set the property to a simple plain text string:
    string myData="test string";

    I hope this helps! Let us know if you are still facing issues in implementing this.

    Regards,
    Petya
    Telerik
  3. UI for WPF is Visual Studio 2017 Ready
  4. Ivan
    Ivan avatar
    43 posts
    Member since:
    Nov 2013

    Posted 31 Mar 2014 in reply to Petya Link to this post

    Hi Petya,

    thanks for you answer, i didn't notify that you have posted.
    Good to know about TxtDataProvider. My property is using plain text. I tried with TxtDataProvider but got no success :-(
    <telerik:TxtDataProvider
                x:Key="dataProvider3"
                RichTextBox="{Binding ElementName=richTextBox1}"
                Text="{Binding Definition, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
      
    <telerik:RadRichTextBox
                Name="richTextBox1"
                IsReadOnly="{Binding DefinitionIsReadOnly}"           
                telerik:RadDockPanel.Dock="Top"
                AcceptsTab="True"
                AcceptsReturn="True"/>
    Is there always something wrong with my code? I have looked with my ViewModel. The property got value correctly. But i can not see them in my View.
  5. Ivan
    Ivan avatar
    43 posts
    Member since:
    Nov 2013

    Posted 31 Mar 2014 in reply to Petya Link to this post

    Hi Petya,

    thanks for your answer, i didn't notify that.
    Good to know about TxtDataProvider. I really use plain text for my property.
    But i can not get the text to be shown in my view with this code.
    <UserControl.Resources>
            <telerik:TxtDataProvider
                x:Key="dataProvider3"
                RichTextBox="{Binding ElementName=richTextBox1}"
                Text="{Binding Definition, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
    </UserControl.Resources>
     
    <Grid>
          <telerik:RadRichTextBox
              Name="richTextBox1"
              IsReadOnly="{Binding DefinitionIsReadOnly}"           
              telerik:RadDockPanel.Dock="Top"
              AcceptsTab="True"
              AcceptsReturn="True"/>
    </Grid>
    I have checked my ViewModel, the property "Definition" will be filled correctly.
    Is there always something wrong?

    Thanks a lot
  6. Answer
    Petya
    Admin
    Petya avatar
    975 posts

    Posted 02 Apr 2014 Link to this post

    Hello,

    Please make sure to add the data provider in the visual tree and not in the resources, as with the current setup the provider might not be notified about changes in the data context.

    Additionally, note that in order to make sure the update happens when focus goes out of the control you need to set the UpdateSourceTrigger property of the data provider, i.e.
    <telerik:TxtDataProvider
        RichTextBox="{Binding ElementName=richTextBox1}"
        Text="{Binding Definition, Mode=TwoWay}" UpdateSourceTrigger="LostFocus"/>

    Let me know if you have additional questions.

    Regards,
    Petya
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  7. Ivan
    Ivan avatar
    43 posts
    Member since:
    Nov 2013

    Posted 03 Apr 2014 in reply to Petya Link to this post

    Petya said:

    Please make sure to add the data provider in the visual tree and not in the resources, as with the current setup the provider might not be notified about changes in the data context.


    Thanks Petya, that's the point.

    Regards,
    Ivan
  8. Prithvi
    Prithvi avatar
    19 posts
    Member since:
    Jul 2012

    Posted 01 Sep 2014 in reply to Petya Link to this post

    Hi Petya,

    Pls have a look to the below code: 

      <Grid x:Name="LayoutRoot" Background="White">
            <telerik:RadRichTextBox x:Name="radRichTextBox"/>


            <telerik:TxtDataProvider RichTextBox="{Binding ElementName=radRichTextBox}"
                                     Text="{Binding TxtDoc, Mode=TwoWay}"/>
            
        </Grid>

    though I have used txtDataProvider directly in visual tree, in my output window am not getting the text whc is there in VM.

  9. Petya
    Admin
    Petya avatar
    975 posts

    Posted 03 Sep 2014 Link to this post

    Hello Prithvi,

    I'm not sure what might be going wrong in your project, could you share how is the property in your view model set? Also, please make sure the data context of the view where the RadRichTextBox is placed is set correctly. 

    Attached is a simple sample project for your reference. I hope it is helpful.

    Regards,
    Petya
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
UI for WPF is Visual Studio 2017 Ready