将数据库中的文本数据显示到WPF RichTextBox中



我有以下用户控件xaml,包含多个RichTextBox控件:

<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>

以及下面的Code-Behind,其中我定义了方法ShowTextInRow(字符串textstring(,该方法可以从程序中的其他地方调用,以获取文本字符串并将其显示在名为"RTBSelected"的RichTextBox中:

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://stackoverflow.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);
}
}
}
}

我遇到了以下问题:如果我将"ShowTextInRow(("方法声明为静态,那么codeehind就无法通过其名称"RTBSelected"识别对RichTextBox的引用。另一方面,如果我从"ShowTexdInRow(("方法中删除了"static"声明,则codeehind似乎可以通过其名称"RTBSelected"识别RichTextBox,但我不能再从程序中的其他地方调用此方法。

很抱歉,如果我错过了一些非常基本的东西——我是C#/WPF的新手。提前感谢您的支持。

因为此操作是UserControl的一部分,所以您不希望将其称为静态操作。当您试图加载到不存在的RichTextBox时,将其称为static会给您带来其他错误。在对UserControl进行任何操作之前,必须对其进行初始化。

删除静态声明,并通过首先初始化UserControl从程序的其他区域调用它。

已删除静态声明:

public void ShowTextInRow(string textstring)
{
// Source: https://stackoverflow.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);
}
}

从命名空间内的其他区域调用:

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

最新更新