简体   繁体   中英

Text from Code Code Behind into Textblock with Different Font Style

I have a text block whose text is generated by a converter from a bound object. I am trying to have only part of the text in a different font than the rest. Is this possible in Silverlight/WPF?

How would I go about generating code to produce this effect?

Example Text:

-Hello, this is some text- "More text"

Where any text in -- is one font, and "" denotes another font. I looked into inline runs, but that did not seem feasible with code behind.

Can anyone point me in the right direction?

Thanks, Ty

You're correct to do Inlines

        textBlock.Inlines.Add(new Run
                           {
                               FontFamily = new FontFamily("Comic Sans"),
                               Text = "Your text"
                           });
        textBlock.Inlines.Add(new Run
        {
            FontFamily = new FontFamily("Tahoma"),
            Text = " is different"
        });

One possible other solution if you already know what the text will be (you know that there will be two portions of text) is to have some pre-canned xaml ready to go and use the XamlReader.Parse method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM