Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change font color from C# in WPF

Tags:

c#

wpf

I have created a simple Calendar application and I would like to change the color of names of the days that are displayed. I created a simple condition:

if (nameDay.Text.Equals("Sunday"))
{
    daytxt.Foreground = Brushes.Red;
}

But in this case the color is changing permanently. When the name of day changes to "Monday" then color of the text is still red but it should be black. How can I fix my issue?

like image 477
Luki Avatar asked Oct 30 '16 20:10

Luki


People also ask

How do you change the font color in C++?

If You want to change the Text color in C++ language There are many ways. In the console, you can change the properties of output. click this icon of the console and go to properties and change color. The second way is calling the system colors.

How do I change the output color in Dev C++?

If you are going to write your program for Windows and you want to change color of text and/or background, use this: SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), attr); Where attr is a combination of values with | (bitwise OR operator), to choose whther you want to change foreground or background color.


1 Answers

An else condition is missing from your if statement in order to achieve what you need.

You can do it 1 of 2 ways:

if (nameDay.Text.Equals("Sunday")) {     daytxt.Foreground = Brushes.Red; } else {     daytxt.Foreground = Brushes.Black; } 

Or

daytxt.Foreground = nameDay.Text.Equals("Sunday") ? Brushes.Red : Brushes.Black; 
like image 68
Bijington Avatar answered Sep 28 '22 03:09

Bijington