我正在制作通用Windows应用程序,其中包括将文本插入文本框。我希望我的应用程序建议文件中的文本插入到文本框中。但我找不到那个属性。我已通过XAML标记在MainPage.xaml中添加了文本框。我相信在WPF API中有这个操作的属性。我只是不确定我能否在UWP中做到这一点。
答案 0 :(得分:4)
我建议对UWP使用AutoSuggestBox控件。一旦用户开始输入文本,自动建议结果列表就会自动填充。结果列表可以显示在文本输入框的上方或下方。
<AutoSuggestBox PlaceholderText="Search" QueryIcon="Find" Width="200"
TextChanged="AutoSuggestBox_TextChanged"
QuerySubmitted="AutoSuggestBox_QuerySubmitted"
SuggestionChosen="AutoSuggestBox_SuggestionChosen"/>
private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
// Only get results when it was a user typing,
// otherwise assume the value got filled in by TextMemberPath
// or the handler for SuggestionChosen.
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
//Set the ItemsSource to be your filtered dataset
//sender.ItemsSource = dataset;
}
}
private void AutoSuggestBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
{
// Set sender.Text. You can use args.SelectedItem to build your text string.
}
private void AutoSuggestBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
if (args.ChosenSuggestion != null)
{
// User selected an item from the suggestion list, take an action on it here.
}
else
{
// Use args.QueryText to determine what to do.
}
}
以下是GitHub回购的link,用于完整的UI基础示例。
希望这有帮助。
答案 1 :(得分:1)
这可能不适用于UAP,但使用WPF时,有一个技巧允许“下拉建议列表”。您可以使用组合框替换文本框,并在用户输入时填充其项目。这可以通过像这样的绑定来实现:
Text={ Binding Path=meCurrentValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }
ItemsSource={Binding Path=meFilteredListOfSuggestions, Mode=TwoWay }
然后在你的viewmodel中你可以做到:
public string meCurrentValue
{
get { return _mecurrentvalue; }
set {
_mecurrentvalue = value;
updateSuggestionsList();
NotifyPropertyChanged("meCurrentValue");
NotifyPropertyChanged("meFilteredListOfSuggestions"); // notify that the list was updated
ComboBox.Open(); // use to open the combobox list
}
public List<string> meFilteredListOfSuggestions
{
get{return SuggestionsList.Select( e => e.text.StartsWith(_mecurrentvalue));}
}
编辑: 请记住将组合框的可编辑值设置为TRUE,这样它将像普通文本框一样工作。