none
Making a part of a label bold

    Question

  • Hey guys,

    I have a WPF application and I need to present some text on the window. I need some of the text to be bold, some underlined, some both and some regular - the end used determines that through a xml file. From the research I conducted, other than using several different labels or to manually implement the label control this is not possible. Is there another control that I can use for this purpose? Any kind of WPF control would be good.

    Thanks!
    Friday, September 05, 2008 9:15 PM

Answers

  • You can also do something like this, but you'll probably end up doing a lot of work to parse and build up the text elements:

     
      <TextBlock> 
        <Span FontWeight="Bold">Hello</Span> 
        <Span FontStyle="Italic">World</Span> 
      </TextBlock> 
     



    Dr. WPF - Online Office at http://drwpf.com/blog/
    • Edited by Dr. WPF Friday, September 05, 2008 9:39 PM simplified
    • Proposed as answer by Marco Zhou Tuesday, September 09, 2008 5:29 AM
    • Marked as answer by Marco Zhou Friday, September 12, 2008 10:05 AM
    Friday, September 05, 2008 9:37 PM
  • This doesn't work for you?

    <Label><TextBlock><Bold>Test</Bold>ing <Underline>123</Underline></TextBlock></Label> 

    -Drew
    • Proposed as answer by Marco Zhou Tuesday, September 09, 2008 5:29 AM
    • Marked as answer by Marco Zhou Friday, September 12, 2008 10:05 AM
    Friday, September 05, 2008 9:45 PM

All replies

  • RichTextBox is your friend.
    Dr. WPF - Online Office at http://drwpf.com/blog/
    Friday, September 05, 2008 9:31 PM
  • You can also do something like this, but you'll probably end up doing a lot of work to parse and build up the text elements:

     
      <TextBlock> 
        <Span FontWeight="Bold">Hello</Span> 
        <Span FontStyle="Italic">World</Span> 
      </TextBlock> 
     



    Dr. WPF - Online Office at http://drwpf.com/blog/
    • Edited by Dr. WPF Friday, September 05, 2008 9:39 PM simplified
    • Proposed as answer by Marco Zhou Tuesday, September 09, 2008 5:29 AM
    • Marked as answer by Marco Zhou Friday, September 12, 2008 10:05 AM
    Friday, September 05, 2008 9:37 PM
  • This doesn't work for you?

    <Label><TextBlock><Bold>Test</Bold>ing <Underline>123</Underline></TextBlock></Label> 

    -Drew
    • Proposed as answer by Marco Zhou Tuesday, September 09, 2008 5:29 AM
    • Marked as answer by Marco Zhou Friday, September 12, 2008 10:05 AM
    Friday, September 05, 2008 9:45 PM
  • Thanks for the replies guys, but I'm using C# programming not XAML programming. Other than that, the content of the text is read from an xml file as a string - something like <Content="TextText">. I can't use <> in my string of content due to xml restrictions, and lets say I do get by that, how can I use your answers with C# programming?

    Thanks again
    Tuesday, October 21, 2008 11:41 PM
  • sample code:

    PartiallyBoldLabel.xaml

    <Window x:Class="DUC.PartiallyBoldLabel"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="PartiallyBoldLabel" Height="300" Width="300">

        <Grid>

            <Label x:Name="lbl" />

        </Grid>

    </Window>



    PartiallyBoldLabel.xaml.cs

    namespace DUC

    {

        /// <summary>

        /// Interaction logic for PartiallyBoldLabel.xaml

        /// </summary>

        public partial class PartiallyBoldLabel : Window

        {

            public PartiallyBoldLabel()

            {

                InitializeComponent();

                //simulating here

                string parsedXmlText1 = "Test";

                string parsedXmlText2 = "ing";

                string parsedXmlText3 = "underline";

                TextBlock tb = new TextBlock();

                tb.Inlines.Add(new Bold(new Run(parsedXmlText1)));

                tb.Inlines.Add(new Run(parsedXmlText2));

                tb.Inlines.Add(new Run("  "));

                tb.Inlines.Add(new Underline(new Run(parsedXmlText3)));

                lbl.Content = tb;

            }

        }

    }


    you can find more examples in:
    http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.aspx

    let me know if this helps.

    Evan Chua-Yap, dotnetfreelanzer at yahoo dot com
    • Proposed as answer by Evan Chua-Yap Wednesday, October 22, 2008 2:41 AM
    Wednesday, October 22, 2008 2:41 AM
  • Yes it's helpful thanks!
    Saturday, June 21, 2014 6:03 PM