[英]Incremental search with dropdown using MahApps.Metro
Can somebody guide me in the right direction for how to implement an incremental search from within a text input, where the alternatives display in a dropdown under the text input field? 有人可以指导我正确的方向,以便如何从文本输入中实现增量搜索,在文本输入字段下的下拉列表中显示替代项吗?
We've been able to implement an incremental search within a dialog, but don't quite see how to design and implement it from a text field. 我们已经能够在对话框中实现增量搜索,但是还不太了解如何从文本字段设计和实现它。 Which components/controls/element do we need to combine, and how to connect these?
我们需要结合哪些组件/控件/元素,以及如何将它们连接起来? We're using version 1.1.2 of MahApps.Metro, and see it as a little troublesome to switch to a pre-alpha version.
我们正在使用MahApps.Metro的1.1.2版本,并认为切换到预Alpha版本有点麻烦。
Say we're looking for names, and we've typed sa
into the field, then we would like to have a drop down with names with that prefix, ie Samantha
, Sara
, Sarah
, Savannah
, more ...
假设我们正在寻找名称,并且已经在字段中输入了
sa
,那么我们希望使用带有该前缀的名称进行下拉,例如Samantha
, Sara
, Sarah
, Savannah
, more ...
In order to implement it you will need a textbox and a popup. 为了实现它,您将需要一个文本框和一个弹出窗口。 There is a very rough example of how to achieve what you want.
有一个非常粗糙的示例,说明如何实现所需的功能。 a ViewModel:
一个ViewModel:
[PropertyChanged.ImplementPropertyChanged]
class SearchViewModel
{
private readonly string[] items;
public string[] SearchResults { get; private set; }
public bool IsSearchShown { get; set; }
public string SearchText { get; set; }
private void OnSearchTextChanged()
{
UpdateSearchResults();
IsSearchShown = true;
}
public string SelectedSearchItem { get; set; }
private void OnSelectedSearchItemChanged()
{
SearchText = SelectedSearchItem;
IsSearchShown = false;
}
private void UpdateSearchResults()
{
SearchResults = items.Where(item => item.StartsWith(SearchText)).ToArray();
}
public SearchViewModel(string[] items)
{
this.items = items;
SearchResults = new string[0];
}
}
view xaml: 查看xaml:
<Grid>
<TextBox Text="{Binding SearchText,UpdateSourceTrigger=PropertyChanged}" x:Name="searchTextBox"/>
<Popup IsOpen="{Binding IsSearchShown}" PlacementTarget="{Binding ElementName=searchTextBox}">
<ListBox ItemsSource="{Binding SearchResults}" SelectedItem="{Binding SelectedSearchItem}"/>
</Popup>
</Grid>
to simplify the code I'm using Fody.PropertyChanged , take into account that it injects calls to OnSelectedSearchItemChanged and OnSearchTextChanged to SelectedSearchItem and SearchText property setters. 为了简化我使用Fody.PropertyChanged的代码,请考虑到它将对OnSelectedSearchItemChanged和OnSearchTextChanged的调用注入到SelectedSearchItem和SearchText属性设置器中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.