private void OnFontFamilyTextBoxTextChanged(object sender, TextChangedEventArgs eventArgs) { // Only handle if changed was caused by user. if (_isInternalUpdate) { return; } // Find matching entry in the list. var items = (IEnumerable <FontFamilyDescription>)_fontFamilyListBox.ItemsSource; var matchingItem = items.FirstOrDefault(item => string.Compare(item.DisplayName, _fontFamilyTextBox.Text, true, CultureInfo.CurrentCulture) == 0); if (matchingItem != null) { SelectedFontFamily = matchingItem.FontFamily; return; } // No exact match found. See if the text is a matching prefix. matchingItem = items.FirstOrDefault(item => item.DisplayName.StartsWith(_fontFamilyTextBox.Text, true, null)); if (matchingItem != null) { SelectedFontFamily = matchingItem.FontFamily; return; } // No prefix match found. Make fuzzy search. float bestMatchValue = -1; FontFamilyDescription bestMatchItem = null; foreach (var item in _fontFamilyListBox.Items.OfType <FontFamilyDescription>()) { float match = StringHelper.ComputeMatch(_fontFamilyTextBox.Text, item.DisplayName); if (match > bestMatchValue) { bestMatchValue = match; bestMatchItem = item; } } if (bestMatchItem != null) { SelectedFontFamily = bestMatchItem.FontFamily; } }
private void CreateFontFamilyDescriptions(object sender, DoWorkEventArgs eventArgs) { var fontFamilies = (ICollection <FontFamily>)eventArgs.Argument; double i = 0; int numberOfFontFamilies = fontFamilies.Count; var items = new List <FontFamilyDescription>(numberOfFontFamilies); foreach (var fontFamily in fontFamilies) { // Check whether user has canceled the loading process. if (_backgroundWorker.CancellationPending) { eventArgs.Result = items; return; } // Load font family. var description = new FontFamilyDescription { DisplayName = FontHelper.GetDisplayName(fontFamily.FamilyNames), FontFamily = fontFamily, IsSymbolFont = FontHelper.IsSymbolFont(fontFamily), }; items.Add(description); // Report progress i++; int progress = (int)(i / numberOfFontFamilies * 100); _backgroundWorker.ReportProgress(progress); } // Sort items by display name. items.Sort((a, b) => string.Compare(a.DisplayName, b.DisplayName, StringComparison.CurrentCulture)); eventArgs.Result = items; }