In an MVVM application I am currently creating, I had the need to embed the WPF WebBrowser control and quickly discovered that the Source Property is not a dependancy object, and so cannot be bound.
I quickly search around on StackOverflow.com and found a neat solution in c#;
http://stackoverflow.com/a/265648/1305169
The VB Version of which is;
1: Namespace Helpers
2:
3: ''' <summary>
4: ''' Allows for the Source Property of the WebBrowser to be Bindable to the DataContext
5: ''' </summary>
6: ''' <remarks></remarks>
7: Public Class WebBrowserUtility
8: Private Sub New()
9: End Sub
10: Public Shared ReadOnly BindableSourceProperty As DependencyProperty = DependencyProperty.RegisterAttached("BindableSource", GetType(String), GetType(WebBrowserUtility), New UIPropertyMetadata(Nothing, AddressOf BindableSourcePropertyChanged))
11:
12: Public Shared Function GetBindableSource(obj As DependencyObject) As String
13: Return DirectCast(obj.GetValue(BindableSourceProperty), String)
14: End Function
15:
16: Public Shared Sub SetBindableSource(obj As DependencyObject, value As String)
17: obj.SetValue(BindableSourceProperty, value)
18: End Sub
19:
20: Public Shared Sub BindableSourcePropertyChanged(o As DependencyObject, e As DependencyPropertyChangedEventArgs)
21: Dim browser As WebBrowser = TryCast(o, WebBrowser)
22: If browser IsNot Nothing Then
23: Dim uri As String = TryCast(e.NewValue, String)
24: browser.Source = If(uri IsNot Nothing, New Uri(uri), Nothing)
25: End If
26: End Sub
27: End Class
28:
29: End Namespace
You then use your new Bindable Property in the XAML as;
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
xmlns:Helpers="clr-namespace:FrontEnd.Helpers"
x:Class="frmKiosk"
x:Name="Window"
Width="1920" Height="1080" WindowStyle="None" WindowStartupLocation="CenterScreen" WindowState="Maximized" Background="#FF1327D0" Cursor="None">
<WebBrowser x:Name="wbMain" Helpers:WebBrowserUtility.BindableSource="{Binding KioskWebAddress}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
</Window>
Then simply creating a Property in the DataContext, and setting the source like;
KioskWebAddress = "c:/HTML/index.html"
No comments:
Post a Comment