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; } } } } }
public TypefaceListItem(Typeface typeface) { _displayName = GetDisplayName(typeface); _simulated = typeface.IsBoldSimulated || typeface.IsObliqueSimulated; this.FontFamily = typeface.FontFamily; this.FontWeight = typeface.Weight; this.FontStyle = typeface.Style; this.FontStretch = typeface.Stretch; string itemLabel = _displayName; if (_simulated) { string formatString = WpfControls.Properties.Resources.ResourceManager.GetString( "simulated", CultureInfo.CurrentUICulture ); itemLabel = string.Format(formatString, itemLabel); } this.Text = itemLabel; this.ToolTip = itemLabel; // In the case of symbol font, apply the default message font to the text so it can be read. if (FontFamilyListItem.IsSymbolFont(typeface.FontFamily)) { TextRange range = new TextRange(this.ContentStart, this.ContentEnd); range.ApplyPropertyValue(TextBlock.FontFamilyProperty, SystemFonts.MessageFontFamily); } }
private void fontFamilyList_SelectionChanged(object sender, SelectionChangedEventArgs e) { FontFamilyListItem item = fontFamilyList.SelectedItem as FontFamilyListItem; if (item != null) { SelectedFontFamily = item.FontFamily; } }
// Update font family list based on selection. // Return list item if there's an exact match, or null if not. private FontFamilyListItem SelectFontFamilyListItem(FontFamily family) { FontFamilyListItem listItem = fontFamilyList.SelectedItem as FontFamilyListItem; if (listItem != null && listItem.FontFamily.Equals(family)) { // Already selected return(listItem); } else if (SelectListItem(fontFamilyList, FontFamilyListItem.GetDisplayName(family))) { // Exact match found return(fontFamilyList.SelectedItem as FontFamilyListItem); } else { // Not in the list return(null); } }
// 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(); } }
private void InitializeFontFamilyList() { ICollection <FontFamily> familyCollection = FontFamilyCollection; if (familyCollection != null) { FontFamilyListItem[] items = new FontFamilyListItem[familyCollection.Count]; int i = 0; foreach (FontFamily family in familyCollection) { items[i++] = new FontFamilyListItem(family); } Array.Sort <FontFamilyListItem>(items); foreach (FontFamilyListItem item in items) { fontFamilyList.Items.Add(item); } } }