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.