Import Docx to RadRichTextBox

4 posts, 0 answers
  1. Guillermo
    Guillermo avatar
    2 posts
    Member since:
    Jul 2020

    Posted 04 Aug Link to this post

    Good morning, the binding with RadDocument and Docx drive me crazy. 

    When I click in importCommand, fires ImportDocx() correctly and file docx is imported in Rdoc property but in RadRichTextBox don´t display in view. When I clik in keyboard, the aplicacion pause in set.

    This is my view model:

    private RadDocument rd;
    public RadDocument Rdoc
    {
    get { return rd; }
    set {

            rd = value;
                  OnPropertyChanged();
           }
    }
    public void ImportDocx()
    {
          RadDocument doc = null;
          DocxFormatProvider provider = new DocxFormatProvider();
          using (Stream inputStream = File.OpenRead(“Any File.docx"))
          {
            doc = provider.Import(inputStream);
          }
          Rdoc=doc;
     }

     

    This is my view (Xaml)

    <telerik:DocxDataProvider RichTextBox="{Binding ElementName=radRichTextBox}" Docx="{Binding Path=Rdoc, Mode=TwoWay}"/>           
                <telerik:RadRichTextBox x:Name="radRichTextBox"/>.......

  2. Martin
    Admin
    Martin avatar
    93 posts

    Posted 05 Aug Link to this post

    Hello Guillermo,

    The provided code sample is pretty close to the desired result.

    Binding to DOCX document requires that the property must be of type byte[] (or List<byte>). Such a property should look like this:

    public byte[] Bytes
    {
    	get
    	{
    		return this.bytes;
    	}
    	set
    	{
    		this.bytes = value;
    		this.OnPropertyChanged();
    	}
    }

    the import method:

    public void ImportDocx()
    {
    	RadDocument doc = new RadDocument();
    	DocxFormatProvider provider = new DocxFormatProvider();
    	using (Stream inputStream = File.OpenRead("SampleDocument.docx"))
    	{
    		MemoryStream memoryStream = new MemoryStream();
    		inputStream.CopyTo(memoryStream);
    		this.Bytes = memoryStream.ToArray();
    	}
    }

    and the XAML:

    <telerik:DocxDataProvider RichTextBox="{Binding ElementName=radRichTextBox}" Docx="{Binding Path=Bytes, Mode=TwoWay}"/>
    <telerik:RadRichTextBox Grid.Row="0" x:Name="radRichTextBox"/>
    <Button Grid.Row="1" Content="Import DOCX" Command="{Binding ClickCommand}"/>

    In order to bind a WPF button to a command in the ViewModel, you can implement the ICommand interface. I am attaching a complete example of the described functionality. Please, feel free to modify it in a way closer to your scenario.

    Regards,
    Martin
    Progress Telerik
  3. Guillermo
    Guillermo avatar
    2 posts
    Member since:
    Jul 2020

    Posted 05 Aug in reply to Martin Link to this post

    Thanks, Martin.

    I also came to that conclusion. But somewhere does it say that binding should be done like this?

    I only found simple examples!!!

    Thanks again

  4. Martin
    Admin
    Martin avatar
    93 posts

    Posted 10 Aug Link to this post

    Hello Guillermo,

    This specific exception is mentioned as a note in Data Providers help article ("An exception to this rule is the Docx property of DocxDataProvider which is a byte array."). I understand that it is nor clear enough and I already wrote down in our backlog to provide more information and examples in our documentation about this data binding.

    Regards,
    Martin
    Progress Telerik

Back to Top