0

我想ComboBox在表单上有一个控件,该表单将用于在用户键入时搜索投资列表。如果我在启动时缓存数据库中的整个投资集合(目前大约 3,000 个项目),我可以轻松地做到这一点,但如果没有必要,我宁愿不这样做。

我试图实现的行为是:

  • 用户在可编辑的 ComboBox 中键入文本。
  • 当用户输入每个字符时,就会触发数据库搜索功能,每次连续击键都会缩小搜索结果的范围。
  • 随着搜索结果的更新,下拉面板将打开并显示相关匹配项

我尝试将 的Text属性绑定ComboBox到 my 上的InvestmentName(字符串)属性ViewModel,并将 的ItemsSource属性绑定到 myComboBox上的InvestmentList(通用列表)属性ViewModel。当我这样做时,Text属性会从 自动完成ItemsSource,但下拉列表显示为空。

我已经能够使用TextBox堆叠在 a上来实现这些结果ListBox,但它不是很优雅,而且它占用了更多的屏幕空间。我还能够让它与TextBox堆叠在 a 顶部一起使用ComboBox,尽管当存在有效搜索项ComboBox时属性设置为“true”时,它会窃取焦点。IsDropDownOpen为此使用两个控件在视觉上也不是很令人愉悦。

我觉得我真的很接近让它按照我想要的方式工作,但有些事情让我无法理解。

此控件的 XAML 是:

<ComboBox Height="23" Width="260" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left"
          ItemsSource="{Binding InvestmentList}" DisplayMemberPath="FullName"
          IsDropDownOpen="{Binding DoShowInvestmentList}"
          ItemsPanel="{DynamicResource ItemsTemplate}" IsEditable="True" 
          Text="{Binding Path=InvestmentName, Mode=TwoWay,
                 UpdateSourceTrigger=PropertyChanged}" />

相关ViewModel属性为:

    private bool _doShowInvestmentList;
    public bool DoShowInvestmentList
    {
        get { return _doShowInvestmentList; }
        set { if (_doShowInvestmentList != value) { _doShowInvestmentList = value; RaisePropertyChanged("DoShowInvestmentList"); } }
    }

    private List<PFInvestment> _investmentList;
    public List<PFInvestment> InvestmentList
    {
        get { return _investmentList; }
        set { if (_investmentList != value) { _investmentList = value; RaisePropertyChanged("InvestmentList"); } }
    }

    private string _investmentName;
    public string InvestmentName
    {
        get { return _investmentName; }
        set
        {
            if (_investmentName != value)
            {
                _investmentName = value;
                this.InvestmentList = DataAccess.SearchInvestmentsByName(value).ToList();

                if (this.InvestmentList != null && this.InvestmentList.Count > 0)
                    this.DoShowInvestmentList = true;
                else
                    this.DoShowInvestmentList = false;

                RaisePropertyChanged("InvestmentName");
            }
        }
    }

我已经对此进行了相当多的研究,但我还没有找到答案。

4

1 回答 1

0

查看这篇关于 CodeProject 的精彩文章,作者...我 :)

一个可重用的 WPF 自动完成文本框

查看 Google 建议示例的末尾,它与您需要的类似,每次按键都会触发对服务器的另一个查询。

于 2009-12-30T20:13:57.900 回答