Ethan Young

Ethan Young

  • NA
  • 68
  • 1k

Syntax to send text to a RichTextBox

Apr 8 2020 6:14 AM
I have prepared the simple program below to illustrate the problem i am trying to solve.
 
(1) The program structure is as follows, following MVVM pattern:
 
 
 
(2) The output is as follows:
  • An input TextBox at the top of the page takes text input (InputText) from the user. Note that this is a proxy for formatted text that would originate from a database.
  • The code automatically pastes the InputText into the the UserControl with the two TextBox's (middle of the window) using a normal MVVM Binding. This works fine.
  • I am failing to understand how to get the InputText to display in the bottom UserControl comprised of two RichTextBoxes. The reason the bottom UserControl contains two RichTextBoxes is i would like to understand how to refer to one of the RichTextBoxes in the UserControl by its name.
  • The code is obviously a bit silly, but only meant to illustrate the problem with minimal overhead.
  • I think i have the right building blocks based on looking at many examples, but i am stuck in terms of where to locate and use the Method that sends the text to the RichTextBox, either the static one in RichTextBoxModels or the non static one in the MyTwoRichTexTBoxUserControl code behind. The idea is that either of these methods would be called in the MainWindowViewModel InputText Set Property Code.
 
 
(3) The code is as follows (and attached):
 MainWindowView.xaml
  1. <Window x:Class="SimpleExample.Views.MainWindowView"   
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
  5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
  6.         xmlns:local="clr-namespace:SimpleExample.Views"   
  7.         xmlns:viewmodels="clr-namespace:SimpleExample.ViewModels"   
  8.         xmlns:usercontrols="clr-namespace:SimpleExample.UserControls"   
  9.         mc:Ignorable="d"   
  10.         Title="MainWindowView" Height="450" Width="800">   
  11.    
  12.     <Window.DataContext>   
  13.         <viewmodels:MainWindowViewModel />   
  14.     </Window.DataContext>   
  15.        
  16.     <Grid>   
  17.         <Grid.ColumnDefinitions>   
  18.             <ColumnDefinition Width="*" />   
  19.             <ColumnDefinition Width="2*" />   
  20.         </Grid.ColumnDefinitions>   
  21.    
  22.         <Grid.RowDefinitions>   
  23.             <RowDefinition Height="*" />   
  24.             <RowDefinition Height="2" />   
  25.             <RowDefinition Height="2*" />   
  26.             <RowDefinition Height="2" />   
  27.             <RowDefinition Height="2*" />   
  28.         </Grid.RowDefinitions>   
  29.    
  30.         <!-- Input Section -->   
  31.         <TextBlock Text="Input Text:"   
  32.                    Background="LightGray"   
  33.                    Grid.Row="0" Grid.Column="0" Margin="10" VerticalAlignment="Center"    
  34.                    TextAlignment="Right" Padding="5"   
  35.                    FontSize="30"/>   
  36.    
  37.         <TextBox Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged}"   
  38.                  Grid.Row="0" Grid.Column="1" Margin="10" VerticalAlignment="Center"    
  39.                  TextAlignment="Left" Padding="5"   
  40.                  FontSize="30"/>   
  41.    
  42.         <Rectangle Grid.Row="1" Grid.ColumnSpan="2" StrokeThickness="1" Fill="Gray" />   
  43.    
  44.         <!-- Output Section: to TextBox UserControl -->   
  45.         <TextBlock Text="Output to My Two TextBox UserControl"   
  46.                    Background="LightGray"   
  47.                    Grid.Row="2" Grid.Column="0" Margin="10" VerticalAlignment="Center"    
  48.                    TextAlignment="Right" TextWrapping="Wrap" Padding="5"   
  49.                    FontSize="30"/>   
  50.    
  51.         <usercontrols:MyTwoTextBoxUserControl DataContext="{Binding}" Grid.Row="2" Grid.Column="1"/>   
  52.    
  53.         <Rectangle Grid.Row="3" Grid.ColumnSpan="2" StrokeThickness="1" Fill="Gray" />   
  54.    
  55.         <!-- Output Secion: to RichTextBox UserControl -->   
  56.         <TextBlock Text="Output to My Two RichTextBox UserControl"   
  57.                    Background="LightGray"   
  58.                    Grid.Row="4" Grid.Column="0" Margin="10" VerticalAlignment="Center"    
  59.                    TextAlignment="Right" TextWrapping="Wrap" Padding="5"   
  60.                    FontSize="30"/>   
  61.    
  62.         <usercontrols:MyTwoRichTextBoxUserControl DataContext="{Binding}" Grid.Row="4" Grid.Column="1"/>   
  63.    
  64.     </Grid>   
  65. </Window> 
MainWindowView.xaml.cs
  1. using System.Windows;   
  2.    
  3. namespace SimpleExample.Views   
  4. {   
  5.     /// <summary>   
  6.     /// Interaction logic for MainWindowView.xaml   
  7.     /// </summary>   
  8.     public partial class MainWindowView : Window   
  9.     {   
  10.         public MainWindowView()   
  11.         {   
  12.             InitializeComponent();   
  13.         }   
  14.     }   

MainWindowViewModel.cs
  1. using SimpleExample.Interfaces;   
  2.    
  3. namespace SimpleExample.ViewModels   
  4. {   
  5.     public class MainWindowViewModel : ObservableObject   
  6.     {   
  7.         #region Fields   
  8.         private string _inputText;   
  9.         #endregion Fields   
  10.   
  11.         #region Properties   
  12.         public string InputText   
  13.         {   
  14.             get { return _inputText; }   
  15.             set    
  16.             {    
  17.                 _inputText = value;   
  18.                 OnPropertyChanged("InputText");   
  19.                 // ONE OF THE TWO METHODS TO SEND THE INPUTTEXT TO THE RICHTEXTBOX WOULD GO HERE  
  20.             }   
  21.         }   
  22.         #endregion Properties   
  23.     }   

MyTwoTextBoxUserControl.xaml
  1. <UserControl x:Class="SimpleExample.UserControls.MyTwoTextBoxUserControl"   
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  4.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
  5.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    
  6.              xmlns:local="clr-namespace:SimpleExample.UserControls"   
  7.              mc:Ignorable="d"    
  8.              d:DesignHeight="450" d:DesignWidth="800">   
  9.     <Grid>   
  10.         <Grid.RowDefinitions>   
  11.             <RowDefinition Height="*"/>   
  12.             <RowDefinition Height="*"/>   
  13.         </Grid.RowDefinitions>   
  14.    
  15.         <TextBox Text="{Binding InputText}"      
  16.                  TextAlignment="Left" VerticalContentAlignment="Center" FontSize="30"   
  17.                  Grid.Row="0" Margin="10"  Padding="5"/>   
  18.    
  19.         <TextBox Text="{Binding InputText}"      
  20.                  TextAlignment="Left" VerticalContentAlignment="Center" FontSize="30"   
  21.                  Grid.Row="1" Margin="10"  Padding="5"/>   
  22.            
  23.     </Grid>   
  24. </UserControl>
MyTwoTextBoxUserControl.xaml.cs
  1. using System.Windows.Controls;   
  2.    
  3. namespace SimpleExample.UserControls   
  4. {   
  5.     /// <summary>   
  6.     /// Interaction logic for MyTextBoxUserControl.xaml   
  7.     /// </summary>   
  8.     public partial class MyTwoTextBoxUserControl : UserControl   
  9.     {   
  10.         public MyTwoTextBoxUserControl()   
  11.         {   
  12.             InitializeComponent();   
  13.         }   
  14.     }   
  15. }
MyTwoRichTextBoxUserControl.xaml
  1. <UserControl x:Class="SimpleExample.UserControls.MyTwoRichTextBoxUserControl"   
  2.              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
  4.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
  5.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    
  6.              xmlns:local="clr-namespace:SimpleExample.UserControls"   
  7.              mc:Ignorable="d"    
  8.              d:DesignHeight="450" d:DesignWidth="800">   
  9.     <Grid>   
  10.            
  11.         <Grid.RowDefinitions>   
  12.             <RowDefinition Height="*"/>   
  13.             <RowDefinition Height="*"/>   
  14.         </Grid.RowDefinitions>   
  15.    
  16.         <RichTextBox    
  17.                  x:Name="RTBTop"   
  18.                  Grid.Row="0" Margin="10" Padding="5"   
  19.                  VerticalContentAlignment="Center" FontSize="30"/>   
  20.            
  21.         <RichTextBox    
  22.                  x:Name="RTBBottom"   
  23.                  Grid.Row="1" Margin="10" Padding="5"   
  24.                  VerticalContentAlignment="Center" FontSize="30"/>   
  25.    
  26.     </Grid>   
  27. </UserControl>
MyTwoRichTextBoxUserControl.xaml.cs
  1. using System.IO;   
  2. using System.Text;   
  3. using System.Windows;   
  4. using System.Windows.Controls;   
  5. using System.Windows.Documents;   
  6.    
  7. namespace SimpleExample.UserControls   
  8. {   
  9.     /// <summary>   
  10.     /// Interaction logic for MyRichTextBoxUserControl.xaml   
  11.     /// </summary>   
  12.     public partial class MyTwoRichTextBoxUserControl : UserControl   
  13.     {   
  14.         public MyTwoRichTextBoxUserControl()   
  15.         {   
  16.             InitializeComponent();   
  17.         }   
  18.    
  19.         // IS THIS THE RIGHT PLACE TO PUT THIS CODE???  
  20.         public void ShowTextInRTBUserControl_USING_CODE_BEHIND(string textString)  
  21.         {   
  22.             byte[] byteArray = Encoding.ASCII.GetBytes(textString);   
  23.             using (MemoryStream ms = new MemoryStream(byteArray))   
  24.             {   
  25.                 TextRange tr = new TextRange(RTBBottom.Document.ContentStart, RTBBottom.Document.ContentEnd);   
  26.                 tr.Load(ms, DataFormats.Rtf);   
  27.             }   
  28.         }   
  29.     }   

RichTextBoxModels.cs
  1. using System.IO;   
  2. using System.Text;   
  3. using System.Windows;   
  4. using System.Windows.Controls;   
  5. using System.Windows.Documents;   
  6.    
  7. namespace SimpleExample.Models   
  8. {   
  9.     public static class RichTextBoxModels   
  10.     {   
  11.         // IS THIS THE RIGHT PLACE TO USE THIS CODE???  
  12.         public static void ShowTextInRTBUserControl_USING_STATIC_METHOD(string textString, RichTextBox rtb)   
  13.         {   
  14.             byte[] byteArray = Encoding.ASCII.GetBytes(textString);   
  15.             using (MemoryStream ms = new MemoryStream(byteArray))   
  16.             {   
  17.                 TextRange tr = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);   
  18.                 tr.Load(ms, DataFormats.Rtf);   
  19.             }   
  20.         }   
  21.     }   
  22. }
ObservableObject.cs
  1. using System.ComponentModel;   
  2.    
  3. namespace SimpleExample.Interfaces   
  4. {   
  5.     /// <summary>   
  6.     /// Implements INotifyPropertyChanged event   
  7.     /// </summary>   
  8.     public class ObservableObject : INotifyPropertyChanged   
  9.     {   
  10.         #region Fields   
  11.         public event PropertyChangedEventHandler PropertyChanged;   
  12.         #endregion   
  13.   
  14.         #region Methods   
  15.         protected void OnPropertyChanged(string propertyName)   
  16.         {   
  17.             PropertyChanged?.Invoke(thisnew PropertyChangedEventArgs(propertyName));   
  18.         }   
  19.         #endregion   
  20.     }   

Question
 
I don't understand how to use the standard public static void method
 
ShowTextInRTBUserControl_USING_STATIC_METHOD: how do i call this method in the InputText Set property of the MainWindowViewmodel in terms of specifiying in which RichTextBox i want the InputText to be displayed. If I give the name of the RichTextBox "RTBBottom", it is not recognized.
 
OR
 
If i use the similar non-static method in the code-behind MyTwoRichTextBoxUserControl.xaml.cs, then i can specify the bottom RichTextBox by its name, but cannot call the method from the MainWindowViewModel InputText Set property.
 
OR
 
Is there a way to setup a binding as one would do with a TextBox.
 
I am sure i am missing something basic - sorry for this. Appreciate any help.

Attachment: SimpleExample.zip

Answers (3)