/// <summary> /// Key = Enter,内容填充 /// Key = Up且ListBox选中第一项,TextBox设为焦点 /// </summary> /// <param name="smartLB">用于显示列表的ListBox</param> /// <param name="e">KeyEventArgs</param> public static void LBKeyDown(ListBox smartLB, KeyEventArgs e) { if (e.Key == Key.Enter) { FillText(smartLB); e.Handled = true; } else if (e.Key == Key.Up && smartLB.SelectedIndex == 0) { SmartTag a = smartLB.Tag as SmartTag; if (a != null && a._TB != null) { a._TB.Focus(); e.Handled = true; } } }
/// <summary> /// 失去焦点方法 /// TextBox_LostFocus /// </summary> /// <param name="smartLB">用于显示列表的ListBox</param> public static void TBLostFocus(ListBox smartLB, bool Update) { if (Update) { SmartTag smartTag = smartLB.Tag as SmartTag; if (smartTag != null && smartTag._TB != null && smartTag._TB.Tag != null) { string _text = smartTag._TB.Text.Trim(); if (_text.Length != 0 && _text != smartTag._Text) { UpdateMark(smartTag._TB.Tag.ToString(), _text); } } } //隐藏smartLB>>清除Tag绑定的TextBox>>清除集合源 smartLB.Visibility = Visibility.Hidden; smartLB.Tag = null; smartLB.ItemsSource = null; }
/// <summary> /// 内容填充方法ListBox_PreviewMouseDoubleClick /// </summary> /// <param name="smartLB"></param> public static void FillText(ListBox smartLB) { if (smartLB.SelectedItem != null) { //获取选中的字符串 string itemString = smartLB.SelectedItem.ToString(); //字符串不为空->smartTag不为空->更新所属文本框的文本>>光标移到文本末尾>>继续 if (itemString.Length != 0) { SmartTag smartTag = smartLB.Tag as SmartTag; if (smartTag != null) { smartTag._TB.Text = itemString; smartTag._TB.Focus(); smartTag._TB.SelectionStart = itemString.Length; smartLB.Visibility = Visibility.Hidden; } } } }
/// <summary> /// 内容更改方法 /// TextBox_TextChanged /// </summary> /// <param name="smartLB">用于显示列表的ListBox</param> public static void TBChanged(ListBox smartLB) { //允许运行(线程池中没有延时更新任务) if (allowRun && keyDown) { keyDown = false; SmartTag smartTag = smartLB.Tag as SmartTag; //有smartTag并且里面有markXEList记录 if (smartTag != null && smartTag._List.Count != 0) { //马上建立分线程了,在线程结束前阻止其他调用TBChanged的命令执行 allowRun = false; //在线程池中添加在分线程中执行的方法委托 //采用匿名方法 ThreadPool.QueueUserWorkItem((o) => { //延时,毫米 Thread.Sleep(threadTimeout); //------延时时间到后------ List <string> _StringList; int _ListCount; string keyword = ""; //在控件关联线程中获取控件的Text smartLB.Dispatcher.Invoke(() => keyword = smartTag._TB.Text); //去除关键字前后空白并把字母设为小写 keyword = keyword.Trim().ToLower(); //关键字为空->返回smartMark对应标记(Mark)的所有文本"Text"列表 >> 继续 if (keyword.Length == 0) { _StringList = smartTag._List.Select(_func => _func.Text).ToList(); } else//其他->根据关键字检索smartMark对应标记(Mark),返回对应文本"Text"列表>>继续 { _StringList = smartTag._List.Where(_func => _func.Letter.Contains(keyword) || _func.Text.Contains(keyword)).Select(_func => _func.Text).ToList(); } _ListCount = _StringList.Count; //_ListCount为0,_ListCount为1并且和senderTB相同->隐藏smartLB >> 返回 if (_ListCount == 0 || _ListCount == 1 && _StringList.Contains(keyword)) { //在控件关联线程中更新控件 smartLB.Dispatcher.Invoke(() => smartLB.Visibility = Visibility.Hidden); } else { //_StringList总数超过MaxDisplayCount->截取指定数量 >> 修改列表总数 >> 继续 if (maxDisplayCount != 0 && maxDisplayCount < _ListCount) { _StringList = _StringList.Take(maxDisplayCount).ToList(); } //绑定数据源 >> 显示smartLB //在控件关联线程中更新控件 smartLB.Dispatcher.Invoke(() => { smartLB.ItemsSource = _StringList; smartLB.Visibility = Visibility.Visible; }); } allowRun = true; }); } else { keyDown = false; smartLB.Visibility = Visibility.Hidden; } } }