简体   繁体   中英

WPF Dynamically format text in a textblock

I feel like I'm going to reinvent wheel so I would like to know if WPF has bult-in support for what I'm trying to achieve. I'm building an app that will allow people to enter some text in a textbox, and then see it formatted in a textblock.

I would like that the user be able to format the text himself by inputing things such as

This [BusinessSpecificStyle] is [/BusinessSpecificStyle] a sample text

My purpose is to be able to easily change the presentation of all my documents by simply changing the underlaying rules in BusinessSpecificStyle. However I don't know what is the best way to implement that with WPF. I was thinking of using a BBCode parser like this one but supposing I go that way, I don't see how I will be able to convert the resulting XAML into TextBlock children programatically, and I seriously wonder if there isn't some kind of built in support for that.

Thanks for your help

IValueConverter is what you are looking for.

Create the converter and format your text based on the bindings passed from the XAML . You can get multiple samples over the net for creating IValueConverter. Refer to the link here and here to get you started.

Not sure if you are asking for Converter here. To me it reads that you want to control the style of a block of text depending on some background and common style?

If that is the case, you want to set the inlines of your text block to seperate your text into run elements, which can reference a specific style resource.

<TextBlock>
  <TextBlock.Inlines>
     <Run>This</Run>
     <Run Foreground="{StaticResource BusinessSpecificStyleForeground}">is</Run>
     <Run>a sample text</Run>
...

in this case, you create a resource which defines the binding styles for run or bind the Style in it's entirety.

Apologies if I am making up a new question, I see you've marked an answer but wanted to add this just in case.

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