簡體   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