using Microsoft.Phone.Controls;
using System;
using System.Windows;
namespace CommonUI.AttachedProperty
{
/// <summary>
/// Atttached property for Webbrowser control,bind a html string to display as web page in web browser
/// </summary>
public class WebbrowserContentBinding
{
/// <summary>
/// attached property Content
/// </summary>
public static readonly DependencyProperty contentproperty = DependencyProperty.Registerattached("Content",typeof(string),typeof(WebbrowserContentBinding),new PropertyMetadata(OnContentChanged));
/// <summary>
/// Get value of attached property Content
/// </summary>
/// <param name="obj">Webbrowser object</param>
/// <returns>the value of Content property </returns>
public static string GetContent(Webbrowser obj)
{
return (string)obj.GetValue(contentproperty);
}
/// <summary>
/// Set the value of attached property Content
/// </summary>
/// <param name="obj">Webbrowser object</param>
/// <param name="content">the value that set to property Content</param>
public static void SetContent(Webbrowser obj,string content)
{
obj.SetValue(contentproperty,content);
}
private static void OnContentChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
{
var webbrowser = d as Webbrowser;
if (webbrowser == null)
{
throw new InvalidOperationException(string.Format("WebbrowserContentBinding attached property only can be used in Webbrowser,you are using in {0}",d.GetType().Name));
}
webbrowser.LoadCompleted += Webbrowser_LoadCompleted;
webbrowser.NavigatetoString(e.NewValue.ToString());
}
private static void Webbrowser_LoadCompleted(object sender,System.Windows.Navigation.NavigationEventArgs e)
{
var wb = sender as Webbrowser;
wb.LoadCompleted -= Webbrowser_LoadCompleted;
wb.Visibility = Visibility.Visible;
}
}
}