zoukankan      html  css  js  c++  java
  • [WPF 学习] 16.*和身份证号码自动加空格显示

    一、定义转换类

       public class IdNoConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string no = value?.ToString() ?? "";
                if (no.Length > 14)
                    return no.Substring(0, 6) + " " + no.Substring(6, 8) + " " + no.Substring(14);
                else if (no.Length > 6)
                    return no.Substring(0, 6) + " " + no.Substring(6);
                return no;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return (value?.ToString() ?? "").Replace(" ", "");
            }
        }
        public class MobileConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string no = value?.ToString() ?? "";
                if (no.Length > 7)
                    return no.Substring(0, 3) + " " + no.Substring(3, 4) + " " + no.Substring(7);
                else if (no.Length > 3)
                    return no.Substring(0, 3) + " " + no.Substring(3);
                return no;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return (value?.ToString() ?? "").Replace(" ", "");
            }
        }
    

    二、添加到资源

        <local:IdNoConverter x:Key="IC"/>
        <local:MobileConverter x:Key="MC"/>
    

    三、文本框设置

            <TextBox Text="{Binding IdNo,Converter={StaticResource IC},UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top"></TextBox>
            <TextBox Text="{Binding Mobile,Converter={StaticResource MC},UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Margin="0,20"></TextBox>
    

    四、结果

  • 相关阅读:
    gradle下载安安装教程
    mybatis 一对一association ,一对多collection
    Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): ModuleBuildError:
    API
    VUE 自定组件,注意事项,注意事件
    IDEA设置
    正则表达式
    前端页面适配的rem换算
    webpack打包原理
    vue-cli脚手架工具根目录的babelrc配置文件
  • 原文地址:https://www.cnblogs.com/catzhou/p/13490806.html
Copyright © 2011-2022 走看看