开发者

Trying to register DependencyProperty for WebBrowser Control

开发者 https://www.devze.com 2023-04-04 21:07 出处:网络
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 Depende

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, WebBrows开发者_C百科er)
    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


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))
0

精彩评论

暂无评论...
验证码 换一张
取 消