How to change font color from C# in WPF

How to change font color from C# in WPF

In WPF, you can change the font color of a control by setting its Foreground property to a Brush object that represents the desired color. Here's how to do it in C#:

using System.Windows.Media; // Get a reference to the control you want to change the font color of var myTextBlock = new TextBlock(); // Create a new SolidColorBrush object with the desired color var brush = new SolidColorBrush(Colors.Red); // Set the Foreground property of the control to the new SolidColorBrush myTextBlock.Foreground = brush; 

In this example, we are creating a new TextBlock control and setting its Foreground property to a new SolidColorBrush object with the color red. You can replace myTextBlock with a reference to any other control that has a Foreground property, such as a Label, Button, or TextBox.

Note that you can also set the Foreground property directly in XAML using a color name, RGB value, or a resource. For example:

<TextBlock Text="Hello, world!" Foreground="Red" /> 
<TextBlock Text="Hello, world!"> <TextBlock.Foreground> <SolidColorBrush Color="Red" /> </TextBlock.Foreground> </TextBlock> 
<SolidColorBrush x:Key="MyBrush" Color="Red" /> <TextBlock Text="Hello, world!" Foreground="{StaticResource MyBrush}" /> 

Regardless of whether you set the Foreground property in XAML or in C#, the end result is the same: the font color of the control will be changed to the specified color.

Examples

  1. "C# WPF change text color dynamically"

    • Code:
      yourTextBlock.Foreground = new SolidColorBrush(Colors.Red); 
    • Description: Sets the text color of a TextBlock to red using a SolidColorBrush.
  2. "WPF change font color based on condition in C#"

    • Code:
      if (someCondition) yourTextBlock.Foreground = new SolidColorBrush(Colors.Green); else yourTextBlock.Foreground = new SolidColorBrush(Colors.Blue); 
    • Description: Changes the text color based on a condition using SolidColorBrush with different colors.
  3. "C# WPF set text color in code-behind"

    • Code:
      yourTextBlock.Foreground = Brushes.Purple; 
    • Description: Sets the text color of a TextBlock to purple using a predefined SolidColorBrush (Brushes).
  4. "WPF change font color in XAML from C#"

    • Code:
      yourTextBlock.SetResourceReference(TextBlock.ForegroundProperty, SystemColors.ControlTextBrushKey); 
    • Description: Changes the text color of a TextBlock by referencing a system color from C# code.
  5. "C# WPF set text color in a ListBox item"

    • Code:
      yourListBoxItem.Foreground = new SolidColorBrush(Colors.Orange); 
    • Description: Sets the text color of a ListBox item to orange using a SolidColorBrush.
  6. "WPF change font color dynamically in a DataGrid"

    • Code:
      yourDataGridRow.Foreground = new SolidColorBrush(Colors.DarkCyan); 
    • Description: Dynamically changes the font color of a DataGrid row to dark cyan using SolidColorBrush.
  7. "C# WPF set text color with hex code"

    • Code:
      yourTextBlock.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF9900")); 
    • Description: Sets the text color of a TextBlock using a hex color code converted to a SolidColorBrush.
  8. "WPF change font color of selected item in ListBox"

    • Code:
      if (yourListBox.SelectedItem != null) ((ListBoxItem)yourListBox.ItemContainerGenerator.ContainerFromItem(yourListBox.SelectedItem)).Foreground = new SolidColorBrush(Colors.Magenta); 
    • Description: Changes the font color of the selected item in a ListBox to magenta.
  9. "C# WPF set text color for a button"

    • Code:
      yourButton.Foreground = new SolidColorBrush(Colors.Gray); 
    • Description: Sets the text color of a Button to gray using a SolidColorBrush.
  10. "WPF change font color in a TextBlock within a DataTemplate"

    • Code:
      DataTemplate template = yourTextBlock.Parent as DataTemplate; if (template != null) template.VisualTree.Foreground = new SolidColorBrush(Colors.Brown); 
    • Description: Changes the font color of a TextBlock within a DataTemplate to brown dynamically.

More Tags

jasmine-node pyside except desktop-application permutation google-sheets-export-url gridsearchcv bottom-sheet tabbedpage zipcode

More C# Questions

More Financial Calculators

More Other animals Calculators

More Dog Calculators

More Chemical thermodynamics Calculators