2016-05-14 39 views
0

我有被設置爲處理事件的GotFocus & 同時框TextChangedAutoSuggestBox。我已清除GotFocus事件中的文本框中的文字。現在問題是,當我選擇AutoSuggestBox中的任何建議時,在選擇它之後調用GotFocus事件處理程序並從中清除所選文本。使用的GotFocus和框TextChanged同時 - Windows Phone的

這是MainPage.xaml代碼使用AutoSuggestBox:

 <AutoSuggestBox 
      x:Name="auto_text_from" 
      HorizontalAlignment="Left" 
      VerticalAlignment="Center" 
      PlaceholderText="Enter Source" 
      Height="auto" 
      Width="280" 
      GotFocus="auto_text_from_GotFocus" 
      TextChanged="AutoSuggestBox_TextChanged"/> 

而這一次是我寫在MainPage.xaml.cs代碼:

private void auto_text_from_GotFocus(object sender, RoutedEventArgs e) 
    { 
     auto_text_from.Text = ""; 
    } 


    string[] PreviouslyDefinedStringArray = new string[] {"Alwar","Ajmer","Bharatpur","Bhilwara", 
     "Banswada","Jaipur","Jodhpur","Kota","Udaipur"}; 


    private void AutoSuggestBox_TextChanged(AutoSuggestBox sender,AutoSuggestBoxTextChangedEventArgs args) 
    { 
     List<string> myList = new List<string>(); 
     foreach (string myString in PreviouslyDefinedStringArray) 
     { 
      if (myString.ToLower().Contains(sender.Text.ToLower()) == true) 
      { 
       myList.Add(myString); 
      } 
     } 
     sender.ItemsSource = myList; 
    } 

我想用這兩個事件處理程序。 GotFocus用於清除文本框的數據,TextChanged用於顯示寫入文本的建議。

請建議我以任何方式做同樣的事情。

感謝提前:)

回答

0

如果AutoSuggestBox有一個事件來處理建議的詞語的選擇,像「SuggestionChosen」,一個不可能性的解決方案是使用不同的處理程序之間管理的專用標誌。

設置一個私有字段:

private bool _isSelectingSuggestion; 

鏈接的方法處理程序一樣OnSuggestionChosen到事件SuggestionChosen並實施這樣的:

private void OnSuggestionChosen(object sender, RoutedEventArgs e) 
{ 
    _isSelectingSuggestion = true; 
} 

然後,在的GotFocus,檢查這樣的標誌:

private void auto_text_from_GotFocus(object sender, RoutedEventArgs e) 
{ 
    if (_isSelectingSuggestion) 
     e.Handled = true; 
    else 
     auto_text_from.Text = ""; 

    _isSelectingSuggestion = false; 
} 

顯然這隻適用於在GotFocus之前提出了:GotFocus開始時,它的結果如下:「好吧,我有焦點,因爲剛剛選擇了一個建議?如果這是真的,我不能清除我的文本!否則,我將清除「

讓我知道這對你這個工作

0

@ MK87:!!是的它與變化一點的工作!:)

private bool _isSelectingSuggestion; 

    private void OnSuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args) 
    { 
     _isSelectingSuggestion = true; 
    } 

    private void auto_text_from_GotFocus(object sender, RoutedEventArgs e) 
    { 
     if (!_isSelectingSuggestion) 
      auto_text_from.Text = ""; 

     _isSelectingSuggestion = false; 
    } 

我刪除此行:

e.Handled == true; 

因爲這是給我的錯誤作爲RoutedEventArgs does not contain a definition for 'Handled'

感謝您的幫助:) :)

+0

... RoutedEventArgs類提供了一個公共屬性,bool Handled,用於設置事件是否已經完全管理,或者是否必須將其轉發給下一個事件處理程序。 –