Reputation: 259
I have the following code in vb.net. I am using WPF and in the XAML i have a Convertor on the Image. Basically the Image based on the Status level should show a particular Image.
I am having trouble with this syntax. I have an error on "ImageSource" as it says "New cannot be used in a class that is MustInherit." I tried removing the New and declaring ImageSource as a String, but the code doesn't return anything into my XAML. What do I need to do?!?
Public Function Convert(ByVal value As Object, _
ByVal targetType As System.Type,
ByVal parameter As Object,
ByVal culture As System.Globalization.CultureInfo) _
As Object Implements System.Windows.Data.IValueConverter.Convert
Dim EstadoIndex As Integer
If Integer.TryParse(value.ToString, EstadoIndex) Then
Select Case EstadoIndex
Case 1
Return New ImageSource("/Cogent;component/Images/Green.png")
Case 2
Return New ImageSource("/Cogent;component/Images/Red.png")
Case Else
Return New ImageSource("/Cogent;component/Images/White.png")
End Select
Else
Return New ImageSource("/Cogent;component/Images/White.png")
End If
End Function
Upvotes: 1
Views: 3617
Reputation: 259
Thanks to OutColdMan and Pondidum,
I had to do a little snopping, but basically both ideas where right on the money. Here is the final code in case anybody is interested!!
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Dim EstadoIndex As Integer
Dim bi As New BitmapImage()
bi.BeginInit()
'Dim ImageSource As String = "/Cogent;component/Images/White.png"
If Integer.TryParse(value.ToString, EstadoIndex) Then
Select Case EstadoIndex
Case 1
bi.UriSource = New Uri("/Cogent;component/Images/Green.png", UriKind.RelativeOrAbsolute)
Return bi
bi.EndInit()
Case 2
bi.UriSource = New Uri("/Cogent;component/Images/Red.png", UriKind.RelativeOrAbsolute)
Return bi
bi.EndInit()
Case Else
bi.UriSource = New Uri("/Cogent;component/Images/White.png", UriKind.RelativeOrAbsolute)
Return bi
bi.EndInit()
End Select
Else
bi.UriSource = New Uri("/Cogent;component/Images/White.png", UriKind.RelativeOrAbsolute)
Return bi
bi.EndInit()
End If
End Function
Upvotes: 1
Reputation: 11617
Checking ImageSource on MSDN shows you need to be using one of its sub-classes, namely a BitmapImage
Upvotes: 0
Reputation: 11832
ImageSource
is an abstract class (I guess MustInherit
this is how you name abstract
in VB.NET). If you want to return image you can use BitmapImage, which will load image in memory for you, use new BitmapImage(new Uri("/Cogent;component/Images/Green.png"))
Also if you need to set it to Image.Source
- you can just return string, Image
will load the image.
Upvotes: 4