70098

WPF Combobox loses Text after Selection

I would like to have a searchable combobox. When I type something into it, the itemlist gets filtered. OnTextChanged does this quite fine. The second part is, inside the comboboxlist all the items are displayed with their shortdescription, but when I select an item, I want the key to be displayed. On SelectionChanged should do that, but everytime I select an item, the combobox input field gets overwritten with "".

private void OnTextChanged(object sender, TextChangedEventArgs e) { ItemSource = new ObservableCollection<RoleKeyElementVM>(DataSource.Where(x => x.ShortDescription.Contains(RoleKeyCombobox.Text) || x.Key.ToString() == RoleKeyCombobox.Text)); RoleKeyCombobox.ItemsSource = ItemSource; } private void OnSelectionChanged(object sender, EventArgs e) { RoleKeyElementVM SelectedItem = RoleKeyCombobox.SelectedItem as RoleKeyElementVM; if(SelectedItem != null) RoleKeyCombobox.Text = SelectedItem.Key.ToString(); }

The selection should look like this: <img src=https://www.e-learn.cn/content/wangluowenzhang/"https://i.stack.imgur.com/s6JK0.jpg" alt="image1">

and the filtering like this

<img src=https://www.e-learn.cn/content/wangluowenzhang/"https://i.stack.imgur.com/l3GbB.jpg" alt="image2">

How can I prevent the combobox from overwriting my custom text with a ""?

Update:

The combobox we are talking about:

<ComboBox Name="RoleKeyCombobox" Margin="5" Grid.Column="2" Grid.Row="0" IsEditable="True" IsSynchronizedWithCurrentItem="False" TextBoxBase.TextChanged="OnTextChanged" SelectionChanged="OnSelectionChanged"> <ComboBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding ShortDescription}"/> </DataTemplate> </ComboBox.ItemTemplate> </ComboBox>

Answer1:

remove OnSelectionChanged

add the following to the RoleKeyElementVM

public override string ToString() { return this.Key; }

better?

Recommend

  • DataBinding with MVVM Light tool kit
  • uwp GridView selectedItem Popup
  • How to add a SearchBar to the top of page like toolbaritems icon with Xamarin.Forms
  • Calling async method from non async method
  • How do I declaratively bind 'SelectedValue' to datasource field?
  • How to transform a Subject into an Observable in RxJs 5
  • ggplot - label as calculated increase (%)
  • Error reading timestamps using “xlsx” package
  • Submit URLs from a data frame column using rvest
  • Create new column based on values in 2 other columns
  • The as.numeric function changes the values in my dataframe [duplicate]
  • Unable to Replicate “R for Beginners” Example
  • compare two cells on same row in datagrid
  • What is the Rx.NET way to produce a cancellable observable of file names?
  • extract unique combinations of subset of parameters from tidy data
  • Reorder stacks in horizontal stacked barplot (R)
  • Can't switch branch: untracked working tree file because of case-insensitivity?
  • R rbind - unexpected symbol error when merging rows from two data frames
  • Apache shutting down unexpectedly
  • How to execute 2 Observables in parallel, ignoring their results and execute next Observable
  • How to access recipient on sent messages page with mailboxer
  • Merge Module leaving files during uninstall
  • Click on button in another program - FindWindow, C#
  • c# open webrowser in many tab
  • How to Cache Real-time Data?
  • Javascript + PHP Encryption with pidCrypt
  • To display the title for the current loaction in map in iphone
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • KeystoneJS: Relationships in Admin UI not updating
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Hits per day in Google Big Query
  • How do I configure my settings file to work with unit tests?
  • Getting Messege Twice Using IMvxMessenger
  • Is it possible to post an object from jquery to bottle.py?
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • How can i traverse a binary tree from right to left in java?
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?