5

I'm adding some TextBlock elements to Border elements in a StackPanel. I'm adding and formating the text of the TextBlock by adding Inlines.

When clicked, I want to get the formated text of the TextBlock.Here is my code.

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 ". As you can see I'm able to get to the bolded Text but how can I ouput it?

St. Helen
  • 99
  • 1
  • 9
  • I think InLines are only valid directly in the XAML. You can create InLines in a converter but it is messy. You could do a FlowDocument in a FlowDocument viewer. – paparazzo May 09 '16 at 12:11
  • To understand your requirement clearly, please tell me you want to get the Bold text (Hello ) on Border_Clicked event, is that right? – Davy May 09 '16 at 12:13
  • Can't you simply set property `FontWeight` as per [this answer](http://stackoverflow.com/a/5263094/1997232) ? Something like `myText.Inlines.Add(new Run("Bold text") { FontWeight = FontWeight.Bold });` – Sinatr May 09 '16 at 12:18
  • @Davy Yes thats exactly want I want to achieve. – St. Helen May 09 '16 at 12:18
  • @Sinatr I didn't tried it. How would I get the text if I make it that way? – St. Helen May 09 '16 at 12:23

3 Answers3

4

@Helen, There is a way to get the Text from the TextPointer using TextRange. 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);
}
Davy
  • 134
  • 5
1

Is the problem to get text out of Bold element?

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;
Sinatr
  • 20,892
  • 15
  • 90
  • 319
0

It is not possible to control Font characteristics in a MessageBox. I think you should consider to create a "custom 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:

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. Take a look at this: showing formatted text

quicoli
  • 612
  • 5
  • 12
  • Thank you but I don't want to output the text in a messagebox but into a `TextBox`. I can't assign `Bold` to `TextBox.Text`. – St. Helen May 09 '16 at 11:58
  • Maybe you should use a RichTextBox for that. Save your Bold element as xaml, and load it in an RichTextBox. See this: https://msdn.microsoft.com/en-us/library/aa970917(v=vs.100).aspx – quicoli May 09 '16 at 12:20