Hello All,
I have one webbrowser control and I took a dependency source property from the Internet and binded the same to my string Url.
I took this dependency property because .Net doesn't lets you bind the Source property to a string datatype and only Uri datatype.
Now I am getting the first page as google.com (set as static for now), but as I change something in the Url textbox, I want to know why this property doesn't updates in the dependency property class also.
My code is as follows :-
Dependency Property Class :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
namespace KMBrowser.KM_GenericClasses
{
public static class WebBrowserUtility
{
public static readonly DependencyProperty BindableSourceProperty = DependencyProperty.RegisterAttached("BindableSource", typeof(string), typeof(WebBrowserUtility), new UIPropertyMetadata(null, OnBindableSourceChanged));
public static string GetBindableSource(DependencyObject obj)
{
return (string)obj.GetValue(BindableSourceProperty);
}
public static void SetBindableSource(DependencyObject obj, string value)
{
obj.SetValue(BindableSourceProperty, value);
}
private static void OnBindableSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
var browser = o as WebBrowser;
if (browser == null)
return;
var uri = (string)e.NewValue;
try
{
browser.Source = !string.IsNullOrEmpty(uri) ? new Uri(uri) : null;
}
catch (ObjectDisposedException) { }
}
}
}
xaml :-
<WebBrowser x:Name="MyWebBrowser" Grid.Row="1" Generics:WebBrowserUtility.BindableSource="{Binding Path=PageSource, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
Property in ViewModel :-
public string PageSource
{
get { return _pageSource; }
set
{
_pageSource = value;
OnPropertyChanged("PageSource");
}
}
Please help, if possible. Thanks a lot in advance :)