2012-07-12 143 views
0

好的。所以我碰到了几个代码示例,声明我可以为WPF WebBrowser控件创建一个自定义属性,这将允许我将一串html绑定到控件进行呈现。WPF WebBrowser控件自定义属性

下面是属性的类(这是在一个名为BrowserHtmlBinding.vb文件):

Public Class BrowserHtmlBinding 

Private Sub New() 
End Sub 

Public Shared BindableSourceProperty As DependencyProperty = 
    DependencyProperty.RegisterAttached("Html", 
             GetType(String), 
             GetType(WebBrowser), 
             New UIPropertyMetadata(Nothing, 
                   AddressOf BindableSourcePropertyChanged)) 

Public Shared Function GetBindableSource(obj As DependencyObject) As String 
    Return DirectCast(obj.GetValue(BindableSourceProperty), String) 
End Function 

Public Shared Sub SetBindableSource(obj As DependencyObject, value As String) 
    obj.SetValue(BindableSourceProperty, value) 
End Sub 

Public Shared Sub BindableSourcePropertyChanged(o As DependencyObject, e As DependencyPropertyChangedEventArgs) 
    Dim webBrowser = DirectCast(o, System.Windows.Controls.WebBrowser) 
    webBrowser.NavigateToString(DirectCast(e.NewValue, String)) 
End Sub 

End Class 

而XAML中:

<Window x:Class="Details" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:custom="clr-namespace:BrowserHtmlBinding" 
Title="Task Details" Height="400" Width="800" Icon="/v2Desktop;component/icon.ico" 
WindowStartupLocation="CenterScreen" WindowStyle="ThreeDBorderWindow" 
WindowState="Maximized"> 
    <Grid> 
     <WebBrowser custom:Html="&lt;b&gt;Hey Now&lt;/b&gt;" /> 
    </Grid> 
</Window> 

我不断收到错误:错误1在'WebBrowser'类型中找不到'Html'属性。

我该如何解决这个问题?它把我拉上了墙!

回答

2

你在你的xmlns映射上市名作为命名空间,然后你不上市,在实际附加属性使用的类名。我无法从你的代码片断中知道你的命名空间是什么(你可以检查项目属性来查找Root命名空间),但假设其类似于WpfApplication1,那么xaml将如下所示。

<Window x:Class="Details" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:custom="clr-namespace:WpfApplication1" 
    Title="Task Details" Height="400" Width="800" 
    Icon="/v2Desktop;component/icon.ico" WindowStartupLocation="CenterScreen" 
    WindowStyle="ThreeDBorderWindow" WindowState="Maximized"> 
<Grid> 
    <WebBrowser custom:BrowserHtmlBinding.Html="&lt;b&gt;Hey Now&lt;/b&gt;" /> 
</Grid> 

+0

谢谢了! – Kevin 2012-07-13 13:44:39