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

Any help guys ?

I don't quite understand your question but it sounds like you need a listener for your BindableSource DependencyProperty? If this is the case this is what you would need to do:

    public static readonly DependencyProperty BindableSourceProperty = DependencyProperty.RegisterAttached("BindableSource", typeof(string), typeof(WebBrowserUtility)); 

    public WebBrowserUtility()
    {
        // Add a listener to BindableSourceProperty changes
        DependencyPropertyDescriptor pd = DependencyPropertyDescriptor.FromProperty(BindableSourceProperty, typeof(WebBrowserUtility)); 
        pd.AddValueChanged(this, BindableSourceProperty_Changed);
    }

    /// <summary>
    /// Raises property changed when BindableSourceProperty changes
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void BindableSourceProperty_Changed(object sender, EventArgs e)
    {
        // Do what you want to do here when the property changes
        // If others need to be alerted you can also raise a property
        // changed alert if you implement the INotifyPropertyChanged interface
        RaisePropertyChanged("BindableSource");
    }

In your code, you have a DependencyProperty handler in place now but this does not listen for DependencyProperty changes in the way you what, it provides a method for you to validate changes.

public static readonly DependencyProperty BindableSourceProperty = DependencyProperty.RegisterAttached("BindableSource", typeof(string), typeof(WebBrowserUtility), new UIPropertyMetadata(null, OnBindableSourceChanged));

Hope this helps :-)

Thanks a lot Sharron for this one...I'll try to implement the solution and will let u know about the same...Thanks again :)

Hi Sharron,

I am a bit confused and unable to integrate your solution into my "WebBrowserUtility" code...Appreciate a lot for your help however, I am a newbie to this one and it would be great if could possibly help me with the same.

Thanks a lot again :)

Hello I Am working wpf application drag and drop just like paint .net like paint.? paint toolbox are drag and drop like it i Am same application work in wpf any member give me some idea and some link then i esay start work..............................

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.