• 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();
            }
        }
    }
    

      

  • 相关阅读:
    使用CefSharp在.NET中嵌入Google kernel
    在WPF中嵌入WebBrowser可视化页面
    C#获取字符串的拼音和首字母
    .NET Core IdentityServer4实战 第六章-Consent授权页
    .NET Core IdentityServer4实战 第Ⅴ章-单点登录
    .NET Core IdentityServer4实战 第Ⅳ章-集成密码登陆模式
    一个C#程序员学习微信小程序路由的笔记
    C#串口通讯概念以及简单实现
    一个C#程序员学习微信小程序的笔记
    使用Http-Repl工具测试ASP.NET Core 2.2中的Web Api项目
  • 原文地址:https://www.cnblogs.com/ants_double/p/5725364.html
Copyright © 2020-2023  润新知