Setting FontWeight in code? RRS feed

  • Question


    In XAML, font weight can be set to bold by simply doing <TextBlock FontWeight="Bold".... />


    How does one do the equivalent in C#?

    Friday, October 19, 2007 12:11 PM


  • you could do

    txt1.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold);//txt1- textblock


    Friday, October 19, 2007 12:19 PM

All replies

  • you could do

    txt1.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold);//txt1- textblock


    Friday, October 19, 2007 12:19 PM
  • Is this for real?  Does no-one know that you can just set the property?????


    TextBlock txtblk = new TextBlock();

    txtblk.FontWeight = FontWeights.Bold;


    Surely using SetValue is more typing and more obscure than simply setting the property.



    • Proposed as answer by Dan Slacker Friday, February 20, 2009 5:09 AM
    Friday, October 19, 2007 3:46 PM
  • In general, is there a performance cost calling the property accessor (which sets the DP) versus directly setting the DP, or does the compiler inline or otherwise make the call disappear?
    Friday, October 19, 2007 4:36 PM
  • The cost of using a property to get\set a dp is no more than the cost of using a property to set a field.


    Unless you are trying to shave milliseconds off a tight loop with tons of iterations, you are probably better off with the readability of a Property setter


    --Ifeanyi Echeruo [MSFT]

    Friday, October 19, 2007 6:01 PM
  • lol Charles.  I guess some things are easier than meets the eye...


    Monday, October 22, 2007 6:05 PM
  • and what about for <Bold>some bold text here</Bold> ?

    If I want to make only a portion of a string bold, and then assign that string to the Text property of a TextBlock element...  how would I do this programmatically?

    is <Bold />  a real element? 
    Does that mean that I must create assign

    TextBlock.Text = (string)string1 + (Bold)boldString + (string)string2;


    It would be nice if it were easier to tell how to accomplish something in code that the Designer makes quite easy.
    Wednesday, July 9, 2008 6:33 PM
  • Of course it is a FrameworkElement...  but creating one with code is not the most straightforward process.

    Naturally, you cannot convert a string into a Bold.  It seems as though Bold takes only TextPointers to determine what text gets bolded.  This seems really counter-intuitive, though I am certain there is some reason, good or otherwise, for doing this.
    Perhaps I am approaching this from the wrong angle.

    It seems a little cumbersome to be mashing together several TextBlocks just so that I can manage to programmatically bold some font in my interface.
    Wednesday, July 9, 2008 6:45 PM
  • This took way too long to find.  I can't believe it's this bloody complicated.  It seems like I should be able to tell it what text to bold, from what string, and it would do it.  I'll have to write my own little method for that. :P

    Anyhow, this code was found here:

    And the page itself had shockingly little to do with what I was trying to accomplish. I had to find this by looking at each method and property... jeezum.

    // A host paragraph to hold the example Inline elements..

    Paragraph par = new Paragraph();

    // Some arbitrary Inline elements.
    Run run1 = new Run("Text run number 1.");
    Run run2 = new Run("Text run number 2.");
    Bold bold = new Bold(new Run("Bold text."));
    Italic ital = new Italic(new Run("Italic text."));
    Span span1 = new Span(new Run("Span number 1"));
    Span span2 = new Span(new Run("Span number 2"));

    // Add the Inline elements to the Paragraph. Note the order
    // of the inline elements in the paragraph; the order is
    // arbitrary, but the notion of an order is important with
    // respect to what are 'previous' and 'next' elements.
    Wednesday, July 9, 2008 7:34 PM
  • Thank you!

    Tuesday, May 25, 2010 12:43 AM
  • nice
    Monday, July 15, 2013 10:24 AM
  • TextBlock txtblk = new TextBlock();

    txtblk.FontWeight = FontWeights.Bold;

    This works a treat!

    My code has a MPF window with a button on it and when I hover over that button wanted the font weight to change:

    this.btnButton.FontWeight = FontWeights.Bold;


    Saturday, September 27, 2014 6:04 AM