2009-12-30 42 views
0

我想有一個ComboBox控制窗體,將用於搜索投資列表作爲用戶類型。如果我在啓動時緩存數據庫中的所有投資(當前爲3000個左右的項目),那麼我可以輕鬆完成此操作,但如果不需要,我寧願不這樣做。WPF組合框從文本數據庫更新其ItemsSource作爲文本屬性更改

,我想實現的行爲是:

  • 用戶鍵入的文本到編輯的ComboBox。
  • 隨着用戶輸入每個字符,觸發數據庫搜索功能,每次連續按鍵縮小搜索結果的範圍。
  • 作爲搜索結果被更新,下拉麪板打開並顯示相關的匹配

我曾嘗試ComboBoxText屬性綁定到InvestmentName(String)屬性我ViewModelItemsSource財產的ComboBox到我的ViewModel上的InvestmentList(通用列表)屬性。當我這樣做時,Text屬性從ItemsSource自動完成,但下拉列表顯示爲空。

我已經能夠實現使用堆疊在ListBox頂部的TextBox這些結果,但它是不是很優雅,它佔用更多的屏幕房地產。我也可以使用堆疊在ComboBox之上的TextBox,但ComboBoxIsDropDownOpen屬性設置爲「有效」時存在有效搜索項。對此,使用兩個控件也不是非常令人滿意。

我覺得我真的很接近讓它按照我想要的方式工作,但是有些東西沒有我。

的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"); 
      } 
     } 
    } 

我做的這個研究公平一點,但我還沒有完全找到答案呢。

回答

0

退房在CodeProject這篇大文章的...我:)

A Reusable WPF Autocomplete TextBox

望結束對谷歌提出例子,它類似於你所需要的,其中每一個按鍵觸發另一個查詢到服務器。

+0

謝謝!那正是我所期待的。 – TeagansDad 2010-01-07 18:24:29

相關問題