zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
    

      

  • 相关阅读:
    模拟光照中的凹凸纹理原理和应用
    Visual Studio 2010 SP1正式开放下载
    同桌的你网工版
    [转载]同桌的你程序员版
    学习计划:SSIS
    基于Java的HTML解析器
    初次使用NHibernate遇到的问题
    .NET下开源CMS系统汇总
    MyEclipse、Tomcat启动项目报错
    VBA 分文件夹 分excel
  • 原文地址:https://www.cnblogs.com/ants_double/p/5725364.html
Copyright © 2011-2022 走看看