Search code examples
c#wpfwpf-controls

How to add multiple properties in a Textblock programatically using WPF & c#?


How do I add multiple properties, in this case Foreground & FontSize to a particular text in a textblock in WPF using c# code ?

I've tried doing it like below but getting error

mytxtblock.Inlines.Clear();
mytxtblock.Inlines.Add(new Run("Bills pending for Processing: ") { Foreground = Brushes.Aquamarine });
mytxtblock.Inlines.Add(new Run(pendingBills.ToString()) { Foreground = Brushes.LimeGreen } { FontSize = 13 });

Help !


Solution

  • There is one thing that is a syntax error

    Instead of

    mytxtblock.Inlines.Add(
      new Run(pendingBills.ToString()) 
      { 
        Foreground = Brushes.LimeGreen 
      } 
      { 
        FontSize = 13 
      });
    

    you have to use

    mytxtblock.Inlines.Add(
      new Run(pendingBills.ToString()) 
      { 
        Foreground = Brushes.LimeGreen, 
        FontSize = 13 
      });
    

    Whenever you ask, you should also describe the error message itself. I do not know if that is a copy/paste error from you or the real problem you have.