Advertisement
parabola949

Autocomplete ComboBox WPF with 10 item history

Dec 4th, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Will redo in MVVM later, but the quick and dirty method
  2. //Autocomplete with filtering added
  3. //Fixed issue with duplicate and empty history items
  4.  
  5. // In View.xaml:
  6. <ComboBox Name="NovellHistory" HorizontalAlignment="Left" Margin="50,28,0,0" VerticalAlignment="Top" Width="200" IsEditable="True">
  7.             <ComboBox.Style>
  8.                 <Style>
  9.                     <EventSetter Event="TextBox.TextChanged" Handler="History_TextChanged" />
  10.                 </Style>
  11.             </ComboBox.Style>
  12.         </ComboBox>
  13.  
  14.  
  15. // In View.cs:
  16. public NovellDesktopView()
  17.         {
  18.             InitializeComponent();
  19.             NovellHistory.ItemsSource = Settings.NovellHistory;
  20.         }
  21.  
  22.         private void Run_Click(object sender, RoutedEventArgs e)
  23.         {
  24.             var history = Settings.NovellHistory;
  25.             Settings.NovellHistory = AddHistoryItem(history, NovellHistory.Text);
  26.         }
  27.  
  28.         private List<string> AddHistoryItem(List<string> input, string item)
  29.         {
  30.             var output = new List<string>();
  31.             if (!input.Contains(item) && item.Length > 0)
  32.                 output.Add(item);
  33.             output.AddRange(input);
  34.             if (output.Count >= 10)
  35.                 output.RemoveAt(9);
  36.             return output;
  37.         }
  38.  
  39.         private void History_TextChanged(object sender, TextChangedEventArgs e)
  40.         {
  41.             var box = (ComboBox)sender;
  42.             box.ItemsSource = from entry in Settings.NovellHistory where entry.ToLower().Contains(box.Text.ToLower()) select entry;
  43.             box.IsDropDownOpen = true;
  44.         }
  45.  
  46. //In Settings.Designer.cs
  47.         [global::System.Configuration.UserScopedSettingAttribute()]
  48.         [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
  49.         [global::System.Configuration.DefaultSettingValueAttribute("")]
  50.         public System.Collections.Generic.List<string> NovellHistory
  51.         {
  52.             get {
  53.                 return ((System.Collections.Generic.List<string>)(this["NovellHistory"]));
  54.             }
  55.             set {
  56.                 this["NovellHistory"] = value;
  57.             }
  58.         }
  59.  
  60. //In my Settings.cs class (because I hate typing out Properties.Settings.Default.SomeSetting)
  61.         public static List<string> NovellHistory
  62.         {
  63.             get { return Properties.Settings.Default.NovellHistory; }
  64.             set { Properties.Settings.Default.NovellHistory = value; Properties.Settings.Default.Save(); }
  65.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement