2012-07-12 142 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