예제 #1
0
            protected override List <object> _Filter(List <object> _unfilteredData, string _displayMember, string userInput)
            {
                int count = userInput.Length - 1;

                List <object> results     = new List <object>();
                string        userLetters = userInput.ToLower();

                foreach (var obj in _unfilteredData)
                {
                    var itemLetters = GetFilterString(obj, _displayMember).ToLower();
                    if (count < itemLetters.Length)
                    {
                        char currentLetter = itemLetters[count];
                        //Match isch = Regex.Match(currentLetter.ToString(), @"[\u4e00-\u9fa5]");
                        // 如果是中文
                        if (CharUtil.IsChineseLetter(currentLetter))
                        {
                            // (首字母)
                            // c = zhong
                            // 注: pinyin 有多种发音的,每一种都可以
                            List <string> pinyins = PinyinUtil.Chinese2Pinyin(currentLetter);

                            foreach (var pinyin in pinyins)
                            {
                                if (pinyin.ToLower()[0] == userLetters[count])
                                {
                                    results.Add(obj);
                                    // 找到匹配的了
                                    break;
                                }
                            }

                            continue;
                        }
                    }
                }
                return(results);
            }
예제 #2
0
            protected override List <object> _Filter(List <object> _unfilteredData, string _displayMember, string userInput)
            {
                int last = userInput.Length - 1;

                List <object> results = new List <object>();

                userInput = userInput.ToLower();

                foreach (var obj in _unfilteredData)
                {
                    var itemLetters = GetFilterString(obj, _displayMember).ToLower();

                    // 一个一个字符比较
                    // e.g. : itemLetters = zz荣智 userInput = zzrongz
                    int ptr_user = 0;

                    bool isInputMatch = true;

                    for (int ptr_sc = 0; ptr_sc < itemLetters.Count(); ptr_sc++)
                    {
                        //if (CharUtil.IsEnglishLetter(itemLetters[ptr_sc]) && userInput[ptr_sc] == itemLetters[ptr_sc])
                        //{
                        //    ptr_user++;
                        //    continue;
                        //}
                        if (ptr_user > last)
                        {
                            break;
                        }
                        if (CharUtil.IsChineseLetter(itemLetters[ptr_sc]))
                        {
                            List <string> pinyins        = PinyinUtil.Chinese2Pinyin(itemLetters[ptr_sc]);
                            bool          anyPinyinMatch = false;
                            foreach (var pinyin in pinyins)
                            {
                                int sub = pinyin.Length < userInput.Length - ptr_user ? pinyin.Length : userInput.Length - ptr_user;
                                if (pinyin.Substring(0, sub) == userInput.Substring(ptr_user, sub))
                                {
                                    ptr_user      += sub;
                                    anyPinyinMatch = true;
                                    break;
                                }
                            }

                            if (!anyPinyinMatch)
                            {
                                isInputMatch = false;
                                break;
                            }
                        }
                        else if (userInput[ptr_user] == itemLetters[ptr_sc])
                        {
                            ptr_user++;
                            continue;
                        }
                        else
                        {
                            isInputMatch = false;
                            ptr_user++;
                            break;
                        }
                    }

                    // 用户输入多余字符, 认为不匹配
                    if (ptr_user <= last)
                    {
                        isInputMatch = false;
                    }

                    if (isInputMatch)
                    {
                        results.Add(obj);
                    }

                    //char currentLetter = itemLetters[last];
                    ////Match isch = Regex.Match(currentLetter.ToString(), @"[\u4e00-\u9fa5]");
                    //// 如果是中文
                    //if (CharUtil.IsChineseLetter(currentLetter))
                    //{
                    //    // (首字母)
                    //    // c = zhong
                    //    // 注: pinyin 有多种发音的,每一种都可以
                    //    List<string> pinyins = PinyinUtil.Chinese2Pinyin(currentLetter);

                    //    foreach (var pinyin in pinyins)
                    //    {
                    //        if (pinyin.ToLower()[0] == userLetters[last])
                    //        {
                    //            results.Add(obj);
                    //            // 找到匹配的了
                    //            break;
                    //        }
                    //    }

                    //    continue;


                    //}
                }
                return(results);
            }