Formatting text in a TextBlock

zleug 61 Reputation points
2020-09-22T23:00:00.277+00:00

Hi All.
I would like format string for EmployeeTextBlock. I created code

var bc = new BrushConverter();

TextBlock tb = new TextBlock();
tb.FontSize = 24;
tb.TextWrapping = TextWrapping.Wrap;
tb.Foreground = ((Brush)(bc.ConvertFrom("#FF1E59D6")));
tb.Inlines.Add("Edit employee ");
tb.Inlines.Add(new Run(data.Name) { FontWeight = FontWeights.Bold });

but when I assign that formated string to textbock

EmployeeTextBlock = tb;

I didn't get the result. How to fix the problem?

Thanks.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2020-09-23T02:16:52.707+00:00

    You may need to remove the old EmployeeTextBlock and add the new one into the UI.I do a little changes for your code to see the formated TextBlock:
    My Xaml code is:

      <Grid Name="myGrid">  
            <TextBlock Name="EmployeeTextBlock" Width="200" Height="60" Background="Azure" MouseDown="EmployeeTextBlock_MouseDown" ></TextBlock>  
        </Grid>  
    

    The event EmployeeTextBlock_MouseDown code is:

    private void EmployeeTextBlock_MouseDown(object sender, MouseButtonEventArgs e)  
            {  
                var bc = new BrushConverter();  
                TextBlock tb = new TextBlock();  
                tb.FontSize = 24;  
                tb.TextWrapping = TextWrapping.Wrap;  
                tb.Foreground = ((Brush)(bc.ConvertFrom("#FF1E59D6")));  
                tb.Inlines.Add("Edit employee ");  
                tb.Inlines.Add(new Run("data.Name") { FontWeight = FontWeights.Bold });  
      
                myGrid.Children.Remove(EmployeeTextBlock);  
                EmployeeTextBlock = tb;  
                myGrid.Children.Add(EmployeeTextBlock);  
            }  
    

    The result picture is like:
    26721-4.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Viorel 114.5K Reputation points
    2020-09-23T10:59:13.427+00:00

    Try this approach too:

    EmployeeTextBlock.Inlines.Clear();
    EmployeeTextBlock.FontSize = 24;
    EmployeeTextBlock.TextWrapping = TextWrapping.Wrap;
    EmployeeTextBlock.Foreground = (Brush)bc.ConvertFrom( "#FF1E59D6");
    EmployeeTextBlock.Inlines.Add( "Edit employee ");
    EmployeeTextBlock.Inlines.Add( new Run( data.Name) { FontWeight = FontWeights.Bold });
    

    Some of the properties can be set in the Designer (XAML), such as Font, Wrapping, etc. The the coding is simpler.

    Sometimes you can also define a text block in XAML having some preformatted child <Run x:Name="…"> elements. Then you can access a <Run> element directly by name to change its text.

    0 comments No comments