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

问题描述 投票:0回答:1

我有以下用户控件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),可以从程序中的其他地方调用该方法,以获取文本字符串并将其显示在名为“ RTBS​​elected”的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()”方法声明为静态方法,则背后的代码无法识别对名称为“ RTBS​​elected”的RichTextBox的引用。另一方面,如果我从“ ShowTexdInRow()”方法中删除“ static”声明,则背后的代码似乎可以通过其名称“ RTBS​​elected”识别RichTextBox,但我无法再从程序的其他位置调用此方法。 >

对不起,如果我错过了一些非常基础的知识-我是C#/ WPF的新手。预先感谢您的支持。

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

wpf richtextbox
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.