Answered Set color of substring without using richtext box?

destro

Well-known member
Joined
Mar 28, 2020
Messages
46
Programming Experience
1-3
Can I change the color of substrings in a single variable without using richtext box?
Currently I am using labels and want to give different color to a single string.
 
Can I change the color of substrings in a single variable without using richtext box?
That doesn't really make sense as asked. A String is just text. It doesn't contain any information about how that text should be displayed unless it is interpreted as markup, e.g. RTF or HTML. You can instruct a Label how to display the text as a whole, e.g. by specifying a font or colour, but variable formatting takes something more.
Currently I am using labels and want to give different color to a single string.
The Label does have a Content property but then you'd need to assign something to that that could do the variable formatting. There's certainly no standard control that will allow you to load plain text and easily specify how it gets formatted. The RichTextBox does it by loading RTF markup and interpreting that. Another option that is still not the simple magic solution you want but might be easier than RTF is to use some sort of web browser control and use HTML markup to format. That's a bit more human-readable than RTF and web browsers are read-only by default.
 
Out of curiosity, is there some kind of stylistic UI trend that is driving this question? A thing a few days ago, someone asked a similar question in DIC but with regards to DevExpress labels rather than WPF labels. Are there websites or some kind of phone app that everyone is trying to emulate that has this look where labels have multiple styles applied to them?
 
What's wrong with creating a class for your labels, and creating a template around that class of labels?
Or creating a template around your xaml labels if that's where you have then designed?

Anything you can do in a webbrowser control can be done in xaml. Your options in WPF are far from being limited, and in fact, you have a wide variety of ways to approach this in WPF. Nothing some independent search engines cant help with unless someone here has the time to show you an example using custom templates.

Frankly, if you are creating complex UI systems, I would suggest using a asp.net MVC app, and create a desktop wrapper for your web project. This is the most modern approach for developing stylish desktop UI systems today, which most developers are starting to undertake. I myself have been building projects like this for some time now.

I would provide you with an example as I have done in the past, except I am very busy as I am about to release one of my biggest projects of my career year-end. Start with custom templating.
 
What's wrong with creating a class for your labels, and creating a template around that class of labels?
Or creating a template around your xaml labels if that's where you have then designed?

Anything you can do in a webbrowser control can be done in xaml. Your options in WPF are far from being limited, and in fact, you have a wide variety of ways to approach this in WPF. Nothing some independent search engines cant help with unless someone here has the time to show you an example using custom templates.

Frankly, if you are creating complex UI systems, I would suggest using a asp.net MVC app, and create a desktop wrapper for your web project. This is the most modern approach for developing stylish desktop UI systems today, which most developers are starting to undertake. I myself have been building projects like this for some time now.

I would provide you with an example as I have done in the past, except I am very busy as I am about to release one of my biggest projects of my career year-end. Start with custom templating.
Best of luck for your project... As there is a lot of work in my current project, I'll simply go with rtf.
 
@Sheepings I looked into dataTriggers but still can get my head around templating and styles.

My label shows a planet that is in ViewModel but I want to change the color of label content based on what planet it is showing.

A single label can hold multiple planets like this:
Saturn 19°
Jupiter 25°
Venus 65°

Mercury 76°
I want to assign different color to each planet.
 
Last edited:
Okay its not easy as it seems. Suppose "Saturn 19 + "\xb0" + "\n" + "Jupiter 25" + "\xb0" + "\n" + "Venus 65" + "\xb0" + "\n" + "Mercury 76" + "\xb0" + "\n" is one single string displayed in a label as:

Saturn 19°
Jupiter 25°
Venus 65°

Mercury 76°


but I want to display it as
Saturn 19°
Jupiter 25°
Venus 65°

Mercury 76°

Every Planet should have its own color but whole string is in single label binded to a single variable.

How many planets will be there inside the string variable is based on calculation inside a viewmodel. View Model uses iteration to append all planets in this form:
"Saturn 19 + "\xb0" + "\n" + "Jupiter 25" + "\xb0" + "\n" + "Venus 65" + "\xb0"

Can I still use Xaml Templating for such operation?? @Sheepings
 
Last edited:
Part of the issue is that you are using a single label to hold multiple pieces of data -- in this case multiple planets. If you had one label per planet then the problem becomes more manageable. For each label, not only would you bind the planet, but you would also bind the label's color to a converter. The job of the converter is to map the planet to a color.
 
The code I have in post #19 of GlassWizard's thread takes one approach of assigning a color in a view model. I did that because the model and problem domain deals with colors directly.

The approach that most people take is to use a converter. A converter is more appropriate because the model and domain don't really care about colors. It's the view and view model which care about the colors. So in the normal case, you don't want a color to be sitting in your data model. You want the view or the supporting view model to take care of choosing a color. See this blog post to see how that is done.
 

Latest posts

Back
Top Bottom