Reputation: 226
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:
and the filtering like this
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>
Upvotes: 2
Views: 617
Reputation: 616
remove OnSelectionChanged
add the following to the RoleKeyElementVM
public override string ToString()
{
return this.Key;
}
better?
Upvotes: 1