Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding RichTextBox to a string

Tags:

wpf

xaml

This may be a very basic question for some but I am rather stuck and haven't been able to find a direct answer from stack or google. If anyone could help that would be great, Was wondering How do I bind a RichTextbox Text to a string.

So I have a variable on my MVVM called Notes and would like to bind it to the Radrichtextbox.

I was thinking something on the lines of this:

<RichTextBox x:Name="RichTextBox1" Text="{Binding Notes}" />

But there's no Text Methods :S

like image 911
Master Avatar asked Jan 23 '26 01:01

Master


2 Answers

You can use RichTextBox(link) from Extended WPF Toolkit.

Example:

<toolkit:RichTextBox x:Name="_richTextBox" Grid.Row="1" Margin="10" BorderBrush="Gray" Padding="10"
                      Text="{Binding Notes}" 
                      ScrollViewer.VerticalScrollBarVisibility="Auto" />

where toolkit is namespace to Extended WPF Toolkit library:

xmlns:toolkit="http://schemas.xceed.com/wpf/xaml/toolkit" 

Installation guide:

Instructions for using the Extended WPF Toolkit binaries (link):

  1. Install .NET Framework 4.0 or 4.5
  2. Download the ExtendedWPFToolkit_Binaries
  3. Right-click ExtendedWPFToolkit_Binaries.zip -> Properties -> Unblock
  4. Unzip the ExtendedWPFToolkit_Binaries.zip
  5. Add a using statement ("using Xceed.Wpf.Toolkit;") to the top of .cs files
  6. Add a new xmlns (for example, xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit") to the top of XAML files
  7. In your XAML, use the namespace prefix (in the above example, )

Or you can install this library by NuGet (link):

PM> Install-Package Extended.Wpf.Toolkit
like image 148
kmatyaszek Avatar answered Jan 25 '26 19:01

kmatyaszek


Oh yeah. Try this

    <RichTextBox>
        <RichTextBox.Document>
            <FlowDocument>
                <Paragraph>
                    <Run Text="{Binding Path=Notes}" />
                </Paragraph>
            </FlowDocument>
        </RichTextBox.Document>
    </RichTextBox>
like image 31
francorobles Avatar answered Jan 25 '26 20:01

francorobles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!