【问题标题】:Display text data from a database into a WPF RichTextBox将数据库中的文本数据显示到 WPF RichTextBox 中
【发布时间】:2020-04-07 15:53:39
【问题描述】:

我有以下用户控件 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>

... 以及下面的代码,其中我定义了 ShowTextInRow(string 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()”方法声明为静态,则代码隐藏无法通过名称“RTBSelected”识别对 RichTextBox 的引用。另一方面,如果我从“ShowTexdInRow()”方法中删除“静态”声明,则代码隐藏似乎通过其名称“RTBSelected”识别 RichTextBox,但我无法再从程序的其他地方调用此方法。

对不起,如果我错过了一些非常基本的东西 - 我是 C#/WPF 的新手。提前感谢您的支持。

【问题讨论】:

  • 这有帮助吗?
  • 对不起,如果我的问题不清楚:我想知道声明 ShowTextInRow() 的正确语法是什么,这样我既可以被称为静态方法,又可以正确引用 RichTextBox“ RTB 已选”。谢谢。
  • 啊,我的链接丢失了stackoverflow.com/questions/60949139/…
  • 感谢您的提问:RowViewUserControl 出现在程序 MainWindowView.xaml 的 XAML 中。在 MainWindow 视图模型中调用数据库。代码工作正常,因为 MainWindowView 中的许多字段都填充了数据库数据,但 RichTextBox 的字段除外。我唯一的困难是如何将从数据库中成功提取的文本字符串(我通过检查是否可以将文本字符串输出到控制台来确认)发送到 RichTextBox“RTBSelected”。

标签: wpf richtextbox


【解决方案1】:

因为此操作是UserControl 的一部分,所以您不想将其称为静态。将其称为静态会给您带来其他错误,因为您会尝试将其加载到不存在的 RichTextBox 中。 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");

【讨论】:

  • 感谢您的解决方案。正如您所描述的,我现在能够使其工作。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-12
  • 2018-01-24
  • 1970-01-01
相关资源
最近更新 更多