prace magisterskie prace magisterskie - 1 month ago 7
C# Question

How to change font color from C# in 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?

Answer

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;