【发布时间】:2018-05-14 17:10:48
【问题描述】:
我目前正在学习 C# 和 XAML 编程以及使用 MVVM。我已经检查了来自 Microsoft 的官方 AutoSuggestBox 示例。我查看了代码并尝试使用模板 10 实现相同的目标,但没有运气。没有建议弹出。
MainPage.XML:
<AutoSuggestBox RelativePanel.Below="stateTextBox"
x:Name="asb"
PlaceholderText="Type a name (e.g. John)"
DisplayMemberPath="DisplayName"
TextMemberPath="DisplayName"
QueryIcon="Find"
Margin="0,24,0,24"
MinWidth="296"
HorizontalAlignment="Left"
TextChanged="{x:Bind ViewModel.FilterUsuals}"
QuerySubmitted="{x:Bind ViewModel.ProcessQuery}"
SuggestionChosen="{x:Bind ViewModel.ProcessChoice}"
ItemsSource="{Binding Elements}"
/>
MainPageViewModel.cs:
Contact _Contact = default(Contact);
public Contact Contact { get { return _Contact; } set { Set(ref _Contact, value); } }
public void FilterUsuals(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
// We only want to get results when it was a user typing,
// otherwise we assume the value got filled in by TextMemberPath
// or the handler for SuggestionChosen
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
var matchingContacts = ContactSampleDataSource.GetMatchingContacts(sender.Text);
sender.ItemsSource = matchingContacts.ToList();
}
}
public void ProcessQuery(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
if (args.ChosenSuggestion != null)
{
}
else
{
}
}
public void ProcessChoice(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
var contact = (Contact)args.SelectedItem;
}
【问题讨论】:
标签: c# mvvm uwp template10