简体   繁体   English

WPF C# - 从TextBlock获取内联格式化粗体文本

[英]WPF C# - Get inline formated bold Text from TextBlock

I'm adding some TextBlock elements to Border elements in a StackPanel . 我正在向StackPanel Border元素添加一些TextBlock元素。 I'm adding and formating the text of the TextBlock by adding Inlines . 我通过添加Inlines添加和格式化TextBlock的文本。

When clicked, I want to get the formated text of the TextBlock .Here is my code. 单击时,我想获得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 should output "Hello ". Border_Clicked应输出“Hello”。 As you can see I'm able to get to the bolded Text but how can I ouput it? 正如你所看到的,我能够得到粗体文本,但我怎么能输出呢?

@Helen, There is a way to get the Text from the TextPointer using TextRange. @Helen,有一种方法可以使用TextRange从TextPointer获取Text。 Try this code 试试这个代码

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);
}

Is the problem to get text out of Bold element? 问题是从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;
}

If you can add inline like this 如果你可以像这样添加内联

myText.Inlines.Add(new Run("Bold text") { FontWeight = FontWeight.Bold });

then it's 那就是

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

It is not possible to control Font characteristics in a MessageBox. 无法在MessageBox中控制字体特征。 I think you should consider to create a "custom MessageBox". 我认为你应该考虑创建一个“自定义MessageBox”。 Something like this: 像这样的东西:

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

So in its constructor you could send your Bold: 所以在它的构造函数中你可以发送你的Bold:

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

Using: 使用:

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();
}

Now, if you are not sure it will be always a Bold object, I suggest you to save your formatted text in XML and load it after. 现在,如果您不确定它将永远是一个Bold对象,我建议您将格式化的文本保存为XML并在之后加载。 Take a look at this: showing formatted text 看看这个: 显示格式化文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM