依赖属性的强制转换加回调,后期自己再改
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfWebBrowser
{
/// <summary>
/// Interaction logic for UriWindow.xaml
/// </summary>
public partial class UriWindow : Window
{
#region Uri
FrameworkPropertyMetadata frmate = new FrameworkPropertyMetadata();
/// <summary>
/// Uri Dependency Property
/// </summary>
public static readonly DependencyProperty UriProperty =
DependencyProperty.Register ("Uri", typeof (Uri), typeof (UriWindow),
new FrameworkPropertyMetadata (new Uri("http://www.baidu.com"), PropertyChanged, CoerceValue), ValidateValue);
/// <summary>
/// Gets or sets the Uri property. This dependency property
/// indicates ....
/// </summary>
public Uri Uri
{
get { return (Uri) GetValue (UriProperty); }
set
{
SetValue (UriProperty, value);
}
}
//属性改变
static void PropertyChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e)
{
Debug.WriteLine(String.Format("PropertyChanged - 属性:{0} 新值:{1} 旧值:{2}", e.Property.Name, e.NewValue, e.OldValue));
}
//强制转换
static object CoerceValue(DependencyObject dobj, object newValue)
{
Debug.WriteLine(String.Format("CoerceValue - {0}", newValue));
if (newValue.ToString().StartsWith("http://"))
{
return newValue;
}
else
{
return new Uri(@"http://" + newValue.ToString());
}
// return newValue;
}
//验证
static bool ValidateValue(object obj)
{
Debug.WriteLine(String.Format("ValidateValue - {0}", obj));
return true;
}
#endregion
public UriWindow()
{
InitializeComponent();
frmate.CoerceValueCallback = new CoerceValueCallback(CoerceValue);
}
private void btn_Ok_Click (object sender, RoutedEventArgs e)
{
this.DialogResult = true;
this.Close();
}
private void btn_Cancel_Click (object sender, RoutedEventArgs e)
{
this.Close();
}
}
}