private void fontFamilyTextBox_TextChanged(object sender, TextChangedEventArgs e) { string text = fontFamilyTextBox.Text; // Update the current list item. if (SelectFontFamilyListItem(text) == null) { // The text does not exactly match a family name so consider applying auto-complete behavior. // However, only do so if the following conditions are met: // (1) The user is typing more text rather than deleting (i.e., the new text length is // greater than the most recent selection start index), and // (2) The caret is at the end of the text box. if (text.Length > _fontFamilyTextBoxSelectionStart && fontFamilyTextBox.SelectionStart == text.Length) { // Get the current list item, which should be the nearest match for the text. FontFamilyListItem item = fontFamilyList.Items.CurrentItem as FontFamilyListItem; if (item != null) { // Does the text box text match the beginning of the family name? string familyDisplayName = item.ToString(); if (string.Compare(text, 0, familyDisplayName, 0, text.Length, true, CultureInfo.CurrentCulture) == 0) { // Set the text box text to the complete family name and select the part not typed in. fontFamilyTextBox.Text = familyDisplayName; fontFamilyTextBox.SelectionStart = text.Length; fontFamilyTextBox.SelectionLength = familyDisplayName.Length - text.Length; } } } } }
// Update font family list based on selection. // Return list item if there's an exact match, or null if not. private FontFamilyListItem SelectFontFamilyListItem(string displayName) { FontFamilyListItem listItem = fontFamilyList.SelectedItem as FontFamilyListItem; if (listItem != null && string.Compare(listItem.ToString(), displayName, true, CultureInfo.CurrentCulture) == 0) { // Already selected return(listItem); } else if (SelectListItem(fontFamilyList, displayName)) { // Exact match found return(fontFamilyList.SelectedItem as FontFamilyListItem); } else { // Not in the list return(null); } }
// Handle changes to the SelectedFontFamily property private void OnSelectedFontFamilyChanged(FontFamily family) { // If the family list is not valid do nothing for now. // We'll be called again after the list is initialized. if (_familyListValid) { // Select the family in the list; this will return null if the family is not in the list. FontFamilyListItem item = SelectFontFamilyListItem(family); // Set the text box to the family name, if it isn't already. string displayName = (item != null) ? item.ToString() : FontFamilyListItem.GetDisplayName(family); if (string.Compare(fontFamilyTextBox.Text, displayName, true, CultureInfo.CurrentCulture) != 0) { fontFamilyTextBox.Text = displayName; } // The typeface list is no longer valid; update it in the background to improve responsiveness. InvalidateTypefaceList(); } }