private static void RefreshListAsync(TextBox textBox, ListBox listBox, string word, PrefixDictionary<KeyValuePair<string, object>[]> dict, Action callback, StringComparison comparison) { var matches = dict.Search(word, false).Select(found => found.Value).SelectMany(found => found).ToList(); listBox.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate{ var ev = (QueryCandidatesEventHandler)textBox.GetValue(QueryCandidatesProperty); if(ev != null){ var e = new QueryCandidatesEventArgs(word); foreach(var del in ev.GetInvocationList()){ del.DynamicInvoke(new object[]{textBox, e}); if(e.Candidates != null){ matches.AddRange(e.Candidates); } } } })); listBox.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(delegate{ bool isIns = GetIsInsertAutomatically(textBox); listBox.ItemsSource = null; listBox.ItemsSource = matches.ToArray(); SetState(listBox, new AutoCompleteState(){TextBox = textBox}); listBox.SelectionChanged -= ListBox_SelectionChanged; if(isIns){ listBox.SelectionChanged += ListBox_SelectionChanged; } if(listBox.Items.Count > 0){ listBox.SelectedIndex = 0; listBox.ScrollIntoView(listBox.SelectedItem); } if(!isIns){ listBox.SelectionChanged += ListBox_SelectionChanged; } if(callback != null){ callback(); } })); }
private static void QueryDirectoryCandidates(object sender, QueryCandidatesEventArgs e) { string path = e.Query; if(!String.IsNullOrEmpty(path)){ var idx = path.LastIndexOf(Path.DirectorySeparatorChar.ToString()); if(idx > 0){ var dir = path.Substring(0, idx + 1); var name = path.Substring(idx + 1); try{ var dirs = Directory.GetDirectories(dir).Where(file => Path.GetFileName(file).StartsWith(name, StringComparison.OrdinalIgnoreCase)); e.Candidates = dirs.Select(d => new KeyValuePair<string, object>(d, d)).ToArray(); }catch{ e.Candidates = new KeyValuePair<string, object>[0]; } } } }