【发布时间】: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 已选”。谢谢。
-
感谢您的提问:RowViewUserControl 出现在程序 MainWindowView.xaml 的 XAML 中。在 MainWindow 视图模型中调用数据库。代码工作正常,因为 MainWindowView 中的许多字段都填充了数据库数据,但 RichTextBox 的字段除外。我唯一的困难是如何将从数据库中成功提取的文本字符串(我通过检查是否可以将文本字符串输出到控制台来确认)发送到 RichTextBox“RTBSelected”。
标签: wpf richtextbox