繁体   English   中英

无法将文件中的文本建议添加到C#中的文本框中

[英]Unable to add text suggestion from a file to my textbox in C#

我正在制作一个通用Windows应用程序,其中包括将文本插入文本框。 我希望我的应用程序建议文件中的文本插入文本框。 但是我找不到那个财产。 我已经通过XAML标记在MainPage.xaml中添加了文本框。 我相信WPF API中有此操作的属性。 我只是不确定是否可以在UWP中做到这一点。

我建议对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存储库的链接,以获取完整的UI基础知识示例。

希望这可以帮助。

这可能不适用于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,这样它将像普通文本框一样工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM