Home > Enterprise >  how do i change color of label in wpf using vb
how do i change color of label in wpf using vb

Time:09-30

I'm converting an application from winforms to wpf and the app changes the color of some labels. In winforms you'd use lblName.ForeColor = Color.Red. But in WPF the Color.Red isn't correct. I can't seem to find the WPF version of this. Most of the sites I can find use C# instead of VB.

The XAML code for a label that needs to change color:

<Label x:Name="lblMsg" Content="Label4" Foreground="Red" Margin="34,562,542.4,71.8"/>

The VB code that should change the color of this label:

 lblMsg.Foreground = Color.Green

CodePudding user response:

here's solution for your problem:

lblMsg.Foreground = New SolidColorBrush(Colors.Green)

you need to cast the Color type to Brush

CodePudding user response:

The Foreground property's type is System.Windows.Media.Brush. This is a change from Winforms' Color struct, which is what you're trying to refer to.

While other answers indicate you can create a new SolidColorBrush and refer to the color you want either via RGB values or directly referring to the previous Color value, you can also simply refer to the color you want from the System.Windows.Media.Brushes class.

For your implementation, you could simply say:

lblMsg.Foreground = Brushes.Green

CodePudding user response:

Never mind, I figured it out at last. this:

lblMsg.Foreground = Color.Green

should be this:

lblMsg.Foreground = Brushes.Green
  • Related