using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace TradeIdeas.TIProGUI.EnhancedSingleStockWindow { //The PreviewTexInput event handlers for the WPF combobox were not working as desired and instead gave weird anomalies such as converting a character to // uppercase yet keeping the previous character unchanges. So, found this code from the following reference: // https://stackoverflow.com/questions/3945221/wpf-combobox-with-forced-uppercase // https://social.msdn.microsoft.com/Forums/vstudio/en-US/4b963ae7-5032-4856-88a1-32256e664d4d/wpf-combobox-charactercasing?forum=wpf // public static class WPFComboAllCaps { [AttachedPropertyBrowsableForType(typeof(ComboBox))] public static CharacterCasing GetCharacterCasing(ComboBox comboBox) { return (CharacterCasing)comboBox.GetValue(CharacterCasingProperty); } public static void SetCharacterCasing(ComboBox comboBox, CharacterCasing value) { comboBox.SetValue(CharacterCasingProperty, value); } // Using a DependencyProperty as the backing store for CharacterCasing. This enables animation, styling, binding, etc... public static readonly DependencyProperty CharacterCasingProperty = DependencyProperty.RegisterAttached( "CharacterCasing", typeof(CharacterCasing), typeof(WPFComboAllCaps), new UIPropertyMetadata( CharacterCasing.Normal, OnCharacterCasingChanged)); private static void OnCharacterCasingChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { var comboBox = o as ComboBox; if (comboBox == null) return; if (comboBox.IsLoaded) { ApplyCharacterCasing(comboBox); } else { // To avoid multiple event subscription comboBox.Loaded -= new RoutedEventHandler(comboBox_Loaded); comboBox.Loaded += new RoutedEventHandler(comboBox_Loaded); } } private static void comboBox_Loaded(object sender, RoutedEventArgs e) { var comboBox = sender as ComboBox; if (comboBox == null) return; ApplyCharacterCasing(comboBox); //comboBox.Loaded -= comboBox_Loaded; //commented out (as per reply in second link mentioned in the top comments. } private static void ApplyCharacterCasing(ComboBox comboBox) { var textBox = comboBox.Template.FindName("PART_EditableTextBox", comboBox) as TextBox; if (textBox != null) { textBox.CharacterCasing = GetCharacterCasing(comboBox); } } } }