1

I'm trying to get the WebBrowser control to display HTML loaded from a variable. I found this solution Displaying html from string in WPF WebBrowser control so am trying to implement my first DependencyProperty.

Here's my attempt at the implementation:

Public Shared ReadOnly HtmlProperty As DependencyProperty = DependencyProperty.RegisterAttached("Html", GetType(String), GetType(PortraitSingle), New FrameworkPropertyMetadata(OnHtmlChanged))

<AttachedPropertyBrowsableForType(GetType(WebBrowser))> _
Public Shared Function GetHtml(ByVal d As WebBrowser) As String
    Return DirectCast(d.GetValue(HtmlProperty), String)
End Function

Public Shared Sub SetHtml(ByVal d As WebBrowser, ByVal value As String)
    d.SetValue(HtmlProperty, value)
End Sub

Private Shared Sub OnHtmlChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
    Dim wb As WebBrowser = TryCast(d, WebBrowser)
    If wb IsNot Nothing Then
        wb.NavigateToString(TryCast(e.NewValue, String))
    End If
End Sub

and the XAML

<WebBrowser x:Name="HTMLDescription" Grid.RowSpan="2" Margin="10" local:PortraitSingle.Html="{Binding HtmlToDisplay}"></WebBrowser>

First off, the problem I'm having is I'm getting the following errors:

Argument not specified for parameter 'e' of 'Private Shared Sub OnHtmlChanged(d As System.Windows.DependencyObject, e As System.Windows.DependencyPropertyChangedEventArgs)

and

The attachable property 'Html' was not found in type 'PortraitSingle'. (PortraitSingle is the name of my Window).

I can't work out how to get around them.

Also, I can't understand how I actually load my HTML into the WebBrowser control.

Ben

Community
  • 1
  • 1
Ben
  • 1,000
  • 2
  • 15
  • 36

1 Answers1

2

Found my problem.

Needed to add the 'AddressOf' to the DependencyProperty statement

Public Shared ReadOnly HtmlProperty As DependencyProperty = DependencyProperty.RegisterAttached("Html", GetType(String), GetType(PortraitSingle), New FrameworkPropertyMetadata(AddressOf OnHtmlChanged))
Ben
  • 1,000
  • 2
  • 15
  • 36