我想要一个可搜索的组合框。当我输入内容时,项目列表会被过滤掉。 OnTextChanged做得很好。第二部分是,在组合框列表中,所有项目都以短描述显示,但是当我选择一个项目时,我希望显示该键。在SelectionChanged上应该这样做,但每次我选择一个项目时,组合框输入字段都会被“”覆盖。
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();
}
和像这样的过滤
如何阻止组合框用“”?
覆盖我的自定义文本更新
我们正在讨论的组合框:
<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>
答案 0 :(得分:1)
删除OnSelectionChanged
将以下内容添加到RoleKeyElementVM
public override string ToString()
{
return this.Key;
}
更好?