1
votes

I have the following user control xaml, containing multiple RichTextBox controls:

<UserControl x:Class="Organizer.UserControls.RowViewUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Organizer.UserControls"
             xmlns:viewmodels="clr-namespace:Organizer.ViewModels"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800"
             x:Name="ucRow">

    <DockPanel Margin="2 2 2 2" Grid.Row="2" Grid.Column="1">

        <RichTextBox x:Name="RTBMinus5" Margin="1 0 0 3" DockPanel.Dock="Top" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
        <RichTextBox x:Name="RTBMinus4" Margin="1 0 0 3" DockPanel.Dock="Top" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
        <RichTextBox x:Name="RTBMinus3" Margin="1 0 0 3" DockPanel.Dock="Top" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
        <RichTextBox x:Name="RTBMinus2" Margin="1 0 0 3" DockPanel.Dock="Top" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
        <RichTextBox x:Name="RTBMinus1" Margin="1 0 0 3" DockPanel.Dock="Top" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>

        <RichTextBox x:Name="RTBPlus5" Margin="1 0 0 3" DockPanel.Dock="Bottom" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
        <RichTextBox x:Name="RTBPlus4" Margin="1 0 0 3" DockPanel.Dock="Bottom" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
        <RichTextBox x:Name="RTBPlus3" Margin="1 0 0 3" DockPanel.Dock="Bottom" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
        <RichTextBox x:Name="RTBPlus2" Margin="1 0 0 3" DockPanel.Dock="Bottom" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
        <RichTextBox x:Name="RTBPlus1" Margin="1 0 0 3" DockPanel.Dock="Bottom" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>

        <RichTextBox Name="RTBSelected" Margin="1 0 0 3" BorderBrush="Purple" Background="Black" Foreground="White" FontSize="18" />

    </DockPanel>
</UserControl>

... and the following Code Behind where i define the method ShowTextInRow(string textstring) which can be called from somewhere else in the program to take a text string and display it into the RichTextBox named "RTBSelected":

using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace Organizer.UserControls
{
    public partial class RowViewUserControl : UserControl
    {
        public RowViewUserControl()
        {
            InitializeComponent();
        }

        public static void ShowTextInRow(string textstring)
        {
            // Source: https://stackguides.com/questions/15983278/storing-data-of-rich-text-box-to-database-with-formatting
            byte[] byteArray = Encoding.ASCII.GetBytes(textstring);
            using (MemoryStream ms = new MemoryStream(byteArray))
            {
                TextRange tr = new TextRange(RTBSelected.Document.ContentStart, RTBSelected.Document.ContentEnd);
                tr.Load(ms, DataFormats.Rtf);
            }
        }
    }
}

I am stuck with the following problem: if i declare the "ShowTextInRow()" method as static then the codebehind does not recognize the reference to the RichTextBox by its name "RTBSelected". On the other hand, if i remove the 'static' declaration from the "ShowTexdInRow()" method, the codebehind seems to recognize the RichTextBox by its name "RTBSelected" but i can no longer call this method from elsewhere in my program.

Sorry if i missed something very basic - i am new to C#/WPF. Thanks in advance for the support.

1
Does this help at all? - Phoenix Stoneham
Sorry if my question is not clear: i would like to know what is the proper syntax to declare the ShowTextInRow() so that i can both be called as a static method and properly refers to the RichTextBox "RTBSelected". Thanks. - scorpiotomse
Thank you for your questions: the RowViewUserControl appears in the XAML of the program MainWindowView.xaml. The call to the database is made in the MainWindow View Model. The code works fine as many fields in the MainWindowView are getting populated with the database data, except the one for the RichTextBox. My only difficulty is how to send the textstring which is successfully extracted from the database (i confirmed by checking that i can output textstring to the console) to the RichTextBox "RTBSelected". - scorpiotomse

1 Answers

0
votes

Because this operation is part of a UserControl you would not want to call it static. Calling it static would give you other errors as you would attempt to load into a RichTextBox that doesn't exist. The UserControl must be initialized before you do anything with it.

Remove the static declaration and call it from other areas of your program by initializing your UserControl first.

Static declaration removed:

public void ShowTextInRow(string textstring)
{
      // Source: https://stackguides.com/questions/15983278/storing-data-of-rich-text-box-to-database-with-formatting
      byte[] byteArray = Encoding.ASCII.GetBytes(textstring);
      using (MemoryStream ms = new MemoryStream(byteArray))
      {
           TextRange tr = new TextRange(RTBSelected.Document.ContentStart, RTBSelected.Document.ContentEnd);
           tr.Load(ms, DataFormats.Rtf);
      }
}

Calling from other areas within the namespace:

RowViewUserControl rvuc = new RowViewUserControl();
rvuc.ShowTextInRow("Hello World");