【问题标题】:WPF Combobox loses Text after SelectionWPF 组合框在选择后丢失文本
【发布时间】:2017-12-14 11:36:28
【问题描述】:

我想要一个可搜索的组合框。当我在其中输入内容时,项目列表会被过滤。 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>

【问题讨论】:

  • 需要更多信息。附上 xaml 声明和自定义样式(如果存在)
  • 我认为您尝试将一些信息打包到单个文本输入字段中...您能描述一下何时应该将键/搜索文本包含在框中的确切状态吗?

标签: c# wpf combobox


【解决方案1】:

移除 OnSelectionChanged

将以下内容添加到 RoleKeyElementVM

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

更好?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多