WPF C# - 如何从TextBlock中获取带格式的加粗文本

5
我正在将一些TextBlock元素添加到StackPanel中的Border元素中。 我通过添加Inlines来添加和格式化TextBlock的文本。
当点击时,我想获取TextBlock的格式化文本。以下是我的代码。
public void addText()
{
    TextBlock myText = new TextBlock();
    myText.Inlines.Add(new Bold(new Run("Hello ")));
    myText.Inlines.Add("World!");

    Border myBorder = new Border();
    myBorder.Child = myText;
    myBorder.MouseDown += new MouseButtonEventHandler(Border_Clicked);

    myStackPanel.Children.Add(myBorder);
}

private void Border_Clicked(object sender, MouseButtonEventArgs e)
{
    //Border senderBox = (Border)sender;
    //TextBlock senderText = (TextBlock)senderBox.Child;
    //Bold inline = (Bold) senderText.Inlines.ElementAt(0);
    // How to Output "Hello "?
}

Border_Clicked 应该输出 "Hello "。你可以看到我能够获取加粗的文本,但如何输出它呢?


我认为InLines只在XAML中直接有效。你可以在转换器中创建InLines,但这很混乱。你可以在FlowDocument查看器中使用FlowDocument。 - paparazzo
为了更清楚地理解您的需求,请告诉我您想在 Border_Clicked 事件中获得加粗的文本(Hello),对吗? - Davy
你不可以根据这个答案来设置FontWeight属性吗?就像这样:myText.Inlines.Add(new Run("粗体文本") { FontWeight = FontWeight.Bold }); - Sinatr
@Davy 是的,那正是我想要实现的。 - St. Helen
@Sinatr 我还没有尝试过。如果我这样做,我该如何获取文本? - St. Helen
3个回答

4

@Helen,使用TextRange可以从TextPointer获取文本。尝试使用以下代码:

void myBorder_MouseDown(object sender, MouseButtonEventArgs e)
{
    var senderBox = (Border)sender;
    var senderText = (TextBlock)senderBox.Child;
    var inline = (Bold)senderText.Inlines.ElementAt(0);

    var textRange = new TextRange(inline.ContentStart, inline.ContentEnd);
    Console.WriteLine(textRange.Text);
}

1

问题是从 Bold 元素中提取文本吗?

private void Border_Clicked(object sender, MouseButtonEventArgs e)
{
    var border = (Border)sender;
    var textBlock = (TextBlock)border.Child;
    var bold = (Bold)textBlock.Inlines.ElementAt(0);

    // How to Output "Hello "?

    // try
    var output = ((Run)bold).Text;
    // or rather (because Bold is a wrapper)
    var output = ((Run)bold.Inlines[0]).Text;
}

如果您可以像这样添加内联内容
myText.Inlines.Add(new Run("Bold text") { FontWeight = FontWeight.Bold });

然后它就是

var run = (Run)textBlock.Inlines[0];
var output = run.Text;

不知道您可以将Inline转换为Run。 - person27

0

在MessageBox中无法控制字体特性。我认为你应该考虑创建一个“自定义MessageBox”。就像这样:

<Window x:Class="WpfApplication1.CustomMessageBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        SizeToContent="WidthAndHeight" MaxWidth="400">

    <Grid x:Name="GridContent" Margin="10">

    </Grid>
</Window>  

所以在它的构造函数中,你可以发送你的 Bold:

private Bold _text;
public CustomMessageBox(Bold formattedText)
{
   _text = formattedText;
   GridContent.Child = _text;
}

使用:

private void Border_Clicked(object sender, MouseButtonEventArgs e)
{
    Border senderBox = (Border)sender;
    TextBlock senderText = (TextBlock)senderBox.Child;
    Bold inline = (Bold) senderText.Inlines.ElementAt(0);
    var customMsgBox = new CustomMessageBox(inline);
    customMsgBox.ShowModal();
}

现在,如果您不确定它始终是一个Bold对象,我建议您将格式化的文本保存为XML,并在加载后进行查看。请看这个:显示格式化的文本


谢谢,但我不想在消息框中输出文本,而是要输出到“TextBox”中。 我无法将“Bold”分配给“TextBox.Text”。 - St. Helen
也许你应该使用 RichTextBox 来实现。将你的 Bold 元素保存为 xaml,然后在 RichTextBox 中加载它。参考链接:https://msdn.microsoft.com/zh-cn/library/aa970917(v=vs.100).aspx - quicoli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接