× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS
Gayot Fow
Feb 05, 2015
<p>There are lots of different ways to approach this question, but arguably the most prominent is the editable combobox strategy (they ask a lot of questions about this in Microsoft's WPF exam which totally IGNORE the MVVM pattern, but that's an issue for another day). I'll give an example to get you started. The first step is to code up a combobox and datagrid...</p> <pre><code> &lt;StackPanel&gt; &lt;ComboBox IsEditable="True" IsTextSearchEnabled="True" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}" ItemsSource="{Binding MyItems}" &gt; &lt;ComboBox.InputBindings&gt; &lt;KeyBinding Key="Enter" Command="{Binding NotImplementedCommand}"/&gt; &lt;/ComboBox.InputBindings&gt; &lt;/ComboBox&gt; &lt;DataGrid ItemsSource="{Binding DriveList}" AutoGenerateColumns="True" /&gt; &lt;/StackPanel&gt; </code></pre> <p>And then create a ViewModel...</p> <pre><code>public class ViewModel :INotifyPropertyChanged { public ObservableCollection&lt;string&gt; MyItems { get; set; } public ObservableCollection&lt;DriveInfo&gt; DriveList { get; set; } public event PropertyChangedEventHandler PropertyChanged; public ViewModel() { MyItems = new ObservableCollection&lt;string&gt;(); DriveList = new ObservableCollection&lt;DriveInfo&gt;(); foreach (DriveInfo di in DriveInfo.GetDrives()) { DriveList.Add(di); } DriveListCollectionChanged(null, null); DriveList.CollectionChanged += DriveListCollectionChanged; } void DriveListCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { MyItems.Clear(); List&lt;string&gt; temp = new List&lt;string&gt;(); foreach (DriveInfo di in DriveList) {// add 4 columns from the DataGrid to the auto complete source temp.Add(di.Name); temp.Add(di.DriveType.ToString()); if (di.IsReady) { temp.Add(di.DriveFormat); temp.Add(di.TotalSize.ToString()); temp.Add(di.AvailableFreeSpace.ToString()); } } foreach (string s in temp.Distinct()) { MyItems.Add(s); } } private string _mySelectedItem; public string MySelectedItem { get { return _mySelectedItem; } set { if (value != _mySelectedItem) { _mySelectedItem = value; OnPropertyChanged("MySelectedItem"); } } } private void OnPropertyChanged(string s) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(s)); } } } </code></pre> <p>This example interrupts the event where the DataGrid's source is changed and populates the combo box with MULTIPLE columns. If you were just doing a single column, the solution here would vary. There is also some binding esoterica where you can automatically bind the combobox to the data, but its didactic value is questionable given what you have asked and your intent to add multiple columns of heterogeneous strings to a combobox.</p> <p>You'll need to better optimize the event handler before deploying your app because the one shown above is only for demonstration purposes.</p> <p>To wire this up, put this (or its alternate) in your Xaml...</p> <pre><code> &lt;Window.Resources&gt; &lt;wpfApplication3:ViewModel x:Key="ViewModel"/&gt; &lt;/Window.Resources&gt; &lt;Grid DataContext="{StaticResource ViewModel}"&gt; &lt;!-- your xaml --&gt; &lt;/Grid&gt; </code></pre> <p>The ViewModel class above is an MVVM compliant solution, and you would bind an instance of this class to the DataContext of the presenting view.</p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/17217241/Autocomplete%20TextBox%20wpf/17219078">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus