locked
get and set text from a RichEditBox

    Question

  • How can I set and get the text from a RichEditBox

    I already tried this, but it is not working:
    contentText is the x:Name from my RichEditBox

    .h private: Platform::String^* tempOutput;

    .cpp contentText->Document->GetText(Windows::UI::Text::TextGetOptions::FormatRtf, tempOutput);


    Monday, May 20, 2013 8:21 PM

Answers

  • This works for me:

    	String^ tempOutput;
    	contentText->Document->GetText(Windows::UI::Text::TextGetOptions::None, &tempOutput);
    	result->Text = tempOutput;

    and

    	contentText->Document->SetText(Windows::UI::Text::TextSetOptions::None, "Hello World");

    • Marked as answer by Pascal90909 Monday, May 20, 2013 9:45 PM
    Monday, May 20, 2013 9:34 PM

All replies

  • This works for me:

    	String^ tempOutput;
    	contentText->Document->GetText(Windows::UI::Text::TextGetOptions::None, &tempOutput);
    	result->Text = tempOutput;

    and

    	contentText->Document->SetText(Windows::UI::Text::TextSetOptions::None, "Hello World");

    • Marked as answer by Pascal90909 Monday, May 20, 2013 9:45 PM
    Monday, May 20, 2013 9:34 PM
  • Thx a lot its working ;)
    Monday, May 20, 2013 9:46 PM
  • While you did get it working, a better approach would be to use databinding to get and set the text data. I realize that you may have been doing an academic exercise but it's still a good idea to use recommended approaches.

    http://blog.voidnish.com

    Friday, May 24, 2013 3:22 PM
  • Can you maybe give me a databinding example code using a RichEditBox?
    Friday, May 24, 2013 4:06 PM
  • Can you maybe give me a databinding example code using a RichEditBox?

    My mistake there. It seems the RichEditBox was not designed for easy databinding (because of the way it loads and saves data). With some extra work, I am sure it'd be possible to get it working but there's nothing out of the box. I may do some research into this and if I can cook up something that allows easy data binding, I'll follow up via this thread.

    http://blog.voidnish.com

    Friday, May 24, 2013 4:38 PM
  • It's pretty straightforward to create a bindable attached property which sets or gets its text value from the RichEditBox's Document or Selection properties.

    --Rob

    Friday, May 24, 2013 6:02 PM
    Owner
  • Rob,

    That is the way to do it but the devil is in the details!

    Edit:  I got it now.  Here is something that works.

    Xaml:

           <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <TextBlock x:Name="mySource">Hello</TextBlock>
            <RichEditBox Width="500" Height="200" local:Attached.Text="{Binding Text, ElementName=mySource}"/>
        </Grid>

    .h

    #pragma once
    
    #include "MainPage.g.h"
    
    namespace WUX = Windows::UI::Xaml;
    
    namespace AttachedProperty
    {
    	/// <summary>
    	/// An empty page that can be used on its own or navigated to within a Frame.
    	/// </summary>
    	public ref class MainPage sealed
    	{
    	public:
    		MainPage();
    
    	protected:
    		virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;
    	};
    
    	public ref class Attached sealed : public WUX::DependencyObject {
    	private:
    		
    		static Windows::UI::Xaml::Interop::TypeName ownerType;
    		static WUX::PropertyMetadata^ propertyMetadata;
    		static WUX::DependencyProperty^ _TextProperty;
    	public:
    		
    		static property WUX::DependencyProperty^ TextProperty
    		{
    			WUX::DependencyProperty^ get() {
    				return _TextProperty;
    			}
    		};
    		static Platform::String^ Attached::GetText(WUX::UIElement^ element) {
    			return (Platform::String^)element->GetValue(_TextProperty);
    		};
    		static void Attached::SetText(WUX::UIElement^ element, Platform::String^ value) {
    			element->SetValue(_TextProperty,value);
    		};
    
    		static void OnTextChanged(Windows::UI::Xaml::DependencyObject^ d, Windows::UI::Xaml::DependencyPropertyChangedEventArgs^ e);
    	};
    }

    cpp:

    TypeName Attached::ownerType = { Attached::typeid->FullName, TypeKind::Metadata };
    
    DependencyProperty^ Attached::_TextProperty = DependencyProperty::RegisterAttached(
    	"Text", 
    	TypeName(String::typeid), //stringType, 
    	ownerType,
    	ref new PropertyMetadata("",
    	ref new PropertyChangedCallback(&Attached::OnTextChanged))	
    	);
    
    void Attached::OnTextChanged(DependencyObject^ d, DependencyPropertyChangedEventArgs^ e)
    {
    	RichEditBox^ Box = (RichEditBox^) d;
    	Platform::String^ s = (Platform::String^)(e->NewValue);
    	Box->Document->SetText(Windows::UI::Text::TextSetOptions::None, s);
    }



    • Edited by jrboddie Friday, May 24, 2013 7:43 PM Corrected code
    Friday, May 24, 2013 6:40 PM
  • Which part of the setup is failing? Your code looks pretty close to mine except for moving the types into variables.

    Here's my definition:

        DependencyProperty^ RichEditExtensions::_TextProperty = DependencyProperty::RegisterAttached(
            "Text",
            TypeName(String::typeid), 
            TypeName(RichEditExtensions::typeid), 
            ref new PropertyMetadata(
                nullptr, 
                ref new PropertyChangedCallback(OnTextChanged)
            )
        );
     
     

    You'll probably also want to listen to the RichEditBox's TextChange event to update your property.

    --Rob

    Friday, May 24, 2013 8:02 PM
    Owner
  • Thanks, Rob.  As usual, I found a solution soon after the original post.  My problem was in the specification of the TypeName in the RegisterAttached method.

    I edited the code above to reflect the correction.

    Thanks again.

    (C++ is not my native language.)

    Friday, May 24, 2013 9:10 PM
  • Nice work there. It's what I'd have done with WPF or Silverlight, so it's satisfying to know that WinRT is similar enough to have the exact same approaches for similar issues.

    http://blog.voidnish.com

    Friday, May 24, 2013 9:11 PM
  • Hello Rob,

    Can I please know how to update the dependency property based on the changes in RichEditBox's text?  I am able to get set the text with new value of the dependency property.  But I wonder how to get the recently changed text from RichEditBox into the dependency property and update my datamodel?

    Thanks in advance.

    Wednesday, November 13, 2013 11:46 PM