我想有一个ComboBox
控制窗体,将用于搜索投资列表作为用户类型。如果我在启动时缓存数据库中的所有投资(当前为3000个左右的项目),那么我可以轻松完成此操作,但如果不需要,我宁愿不这样做。WPF组合框从文本数据库更新其ItemsSource作为文本属性更改
,我想实现的行为是:
- 用户键入的文本到编辑的ComboBox。
- 随着用户输入每个字符,触发数据库搜索功能,每次连续按键缩小搜索结果的范围。
- 作为搜索结果被更新,下拉面板打开并显示相关的匹配
我曾尝试ComboBox
的Text
属性绑定到InvestmentName
(String)属性我ViewModel
和ItemsSource
财产的ComboBox
到我的ViewModel
上的InvestmentList
(通用列表)属性。当我这样做时,Text
属性从ItemsSource
自动完成,但下拉列表显示为空。
我已经能够实现使用堆叠在ListBox
顶部的TextBox
这些结果,但它是不是很优雅,它占用更多的屏幕房地产。我也可以使用堆叠在ComboBox
之上的TextBox
,但ComboBox
在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");
}
}
}
我做的这个研究公平一点,但我还没有完全找到答案呢。
谢谢!那正是我所期待的。 – TeagansDad 2010-01-07 18:24:29