views:

28

answers:

0

Hey pals,

let's put it short: I'm trying to create a WPF custom control that inherits from WPF Image. I have added a dependency property called "Source Type" to my control so that when databound it sets the "Source" property of the image to what I want.

But the problem is that my method to perform the source changing task (i.e "OnSourceTypeChanged") is never invoked. It looks like my control doesn't consider the value given at databinding a change! Am I missing something? (I bet it's ganna be a stupid mistake!) Here is the code for my class:

Public Class InvoiceTypeImage
Inherits Image

Public Shared ReadOnly SourceTypeProperty As DependencyProperty = _
                       DependencyProperty.Register("SourceType", _
                       GetType(InvoiceType), GetType(InvoiceTypeImage), _
                       New PropertyMetadata(New PropertyChangedCallback(AddressOf OnSourceTypeChanged)))

Private Shared Sub OnSourceTypeChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)

    Dim image = CType(d, InvoiceTypeImage)

    Dim type = CType(e.NewValue, InvoiceType)

    Dim bitmap As New BitmapImage
    bitmap.BeginInit()

    If type = InvoiceType.ForTable OrElse type = InvoiceType.None Then
        bitmap.UriSource = New Uri("Images\ForTableIcon.png", UriKind.Relative)
    ElseIf type = InvoiceType.Customer Then
        bitmap.UriSource = New Uri("Images\CustomerIcon.png", UriKind.Relative)
    ElseIf type = InvoiceType.OnDemand Then
        bitmap.UriSource = New Uri("Images\OnDemandIcon.png", UriKind.Relative)
    End If

    bitmap.EndInit()

    image.Source = bitmap

End Sub

Public Property SourceType() As InvoiceType
    Get
        Return GetValue(SourceTypeProperty)
    End Get
    Set(ByVal value As InvoiceType)
        SetValue(SourceTypeProperty, value)
    End Set
End Property

End Class