wpf 依赖强制回调
依赖属性的强制转换加回调,后期自己再改
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(); } } }
作者:Ants_double
出处:https://www.cnblogs.com/ants_double/
本文版权归作者和博客园所有,欢迎转载。转载请在留言板处留言给我,且在文章标明原文链接,谢谢!
如果您觉得本篇博文对您有所收获,觉得我还算用心,请点击右下角的 [大拇指],谢谢!