Home > Net >  Can't specify a SolidBrushColor
Can't specify a SolidBrushColor

Time:10-30

I define a variable as follows:

x = Color.FromArgb(<some integer>)

and use it as follows when specifying a rectangle:

Dim Cell As New Shapes.Rectangle With
{
    .Fill = New SolidColorBrush(x),
    '......
}

However, this gives an error message:

Value of type 'Color' cannot be converted to 'Color'

What is wrong here?

CodePudding user response:

There are two different Color types.

A SolidColorBrush or brushes in general in WPF expect the latter type.

Public Sub New (color As Color)

Unfortunately, this type does not have an overload for a single int, which means you have to convert it and use this FromArgb method instead:

Public Shared Function FromArgb (a As Byte, r As Byte, g As Byte, b As Byte) As Color

You can use one of the approaches from Convert integer to color in WPF, like this one:

Dim bytes = BitConverter.GetBytes(/* some integer */)
Dim color = Color.FromArgb(bytes(3), bytes(2), bytes(1), bytes(0))
Dim brush = New SolidColorBrush(color)
Dim Cell As New Shapes.Rectangle With
    .Fill = brush
}
  • Related