2013-10-01 38 views
1

我正在創建一個自定義的組合框控件與谷歌搜索結果實現,但我遇到了一些問題,使用箭頭鍵。問題
組合框上/下箭頭鍵問題項目重新填充

詳情我演示用上下箭頭鍵的問題,請觀看此視頻,並採取輸出面板上的外觀以及。

http://screencast.com/t/DFkmlDKR

在視頻中,我試圖尋找「一」(只是一個測試),並返回所有以「A」,然後我按向下和向上箭頭鍵啓動谷歌搜索結果中。在輸出面板中,當我按下這些鍵時,它會顯示突出顯示的項目。
enter image description here

然後我輸入下一個字母「l」。現在,如果您注意到輸出面板上的選擇現在爲「空」,但我仍然按下向下箭頭鍵。
enter image description here

而當您使用鼠標指針再次懸停在該項目上時,它將再次開始工作。
enter image description here

我被這個問題困住了幾天,還沒有想出解決方案。

我已經上傳了這個控件的測試版本,所以你也可以使用它。這是GoogleSuggestionComboBox
enter image description here

我的目標在這裏是使UP &向下箭頭鍵工作的所有時間。

在代碼 enter image description here 的這一部分,我試圖foreach語句所以每次採集重新填充有新的結果後加入

SelectedIndex = 0; 

,它會選擇第一個結果。不幸的是,它沒有奏效。

您可以下載測試代碼,以便播放和測試問題。 http://sdrv.ms/1eWV3Bc,這裏也是ComboBox的代碼。

using GoogleSuggestionComboBox.Model; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Diagnostics; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Input; 

namespace GoogleSuggestionComboBox 
{ 
    public class ComboBoxExC : ComboBox 
    { 
     GoogleSuggest google; 
     TextBox textbox; 
     string _text = string.Empty; 
     string _last_text = string.Empty; 

     public ComboBoxExC() 
     { 
      if (DesignerProperties.GetIsInDesignMode(this)) 
      { 

      } 
      else 
      { 
       this.Loaded += ComboBoxExC_Loaded; 

       google = new GoogleSuggest(); 
       google.OnGoogleSuggestAvailable += google_OnGoogleSuggestAvailable; 

       // since we have OnSelectionChanged "disabled" 
       // we need a way to know if the item in ComboBox is selected using mouse 
       EventManager.RegisterClassHandler(typeof(ComboBoxItem), ComboBoxItem.MouseDownEvent, new MouseButtonEventHandler(OnItemMouseDown)); 
      } 
     } 

     void ComboBoxExC_Loaded(object sender, RoutedEventArgs e) 
     { 
      this.textbox = (TextBox)Template.FindName("PART_EditableTextBox", this); 
     } 

     private void OnItemMouseDown(object sender, MouseButtonEventArgs e) 
     { 
      var comboBoxItem = sender as ComboBoxItem; 
      if (comboBoxItem != null && comboBoxItem.IsHighlighted) 
      { 
       Model_SuggestedQueries m = (Model_SuggestedQueries)comboBoxItem.Content; 
       Go(m.Query); 
      } 
     } 

     protected override void OnSelectionChanged(SelectionChangedEventArgs e) 
     { 
      // don't do anything so the .Text value won't change. 

      //base.OnSelectionChanged(e); 
     } 

     protected override void OnPreviewKeyDown(KeyEventArgs e) 
     { 
      this.IsDropDownOpen = true; 

      base.OnPreviewKeyDown(e); 

      d("key: " + e.Key.ToString()); 

      if (this.SelectedItem != null) 
      { 
       Model_SuggestedQueries m = (Model_SuggestedQueries)this.SelectedItem; 
       d("selected: " + m.Query); 
      } 
      else 
      { 
       d("null"); 
      } 
     } 

     protected override void OnPreviewKeyUp(KeyEventArgs e) 
     { 
      base.OnPreviewKeyUp(e); 

      if (e.Key == Key.Enter) 
      { 
       if (this.SelectedItem == null) 
       { 
        Go(this.Text); 

       } 
       else 
       { 
        Model_SuggestedQueries m = (Model_SuggestedQueries)this.SelectedItem; 
        Go(m.Query); 
       } 
      } 
      else 
      { 
       if (this.Text != this._last_text) 
       { 
        google.LookForSuggestion(this.Text); 

        this._last_text = this.Text; 
       } 
      } 
     } 

     void google_OnGoogleSuggestAvailable(object sender, List<Model.Model_SuggestedQueries> suggestions) 
     { 
      this.Items.Clear(); 

      suggestions.ForEach((a) => 
      { 
       this.Items.Add(a); 
      }); 
     } 

     void d(object a) 
     { 
      Debug.WriteLine(">>> " + a); 
     } 

     void Go(string query) 
     { 
      Process.Start("https://www.google.com.ph/search?q=" + query); 

      // clear suggestions 
      this.Items.Clear(); 
     } 
    } 
} 

MainWindow.xaml

<Window x:Class="GoogleSuggestionComboBox.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:l="clr-namespace:GoogleSuggestionComboBox" 
     Title="MainWindow" Height="133" Width="261" WindowStartupLocation="CenterScreen" 
     > 
    <Grid> 
     <StackPanel Margin="10"> 
      <TextBlock Text="Search" /> 
      <l:ComboBoxExC 
       IsEditable="True" 
       IsTextSearchEnabled="False" 
       TextSearch.TextPath="Query" 
       > 
       <l:ComboBoxExC.ItemTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding Query}" /> 
        </DataTemplate> 
       </l:ComboBoxExC.ItemTemplate> 
      </l:ComboBoxExC> 
     </StackPanel> 
    </Grid> 
</Window> 

謝謝
傑森

+0

感謝@Soner格尼爾 –

+1

不要讓用戶與您的代碼玩。請分享代碼中確實存在問題的部分,並制定直接問題。 – meilke

+0

對我來說,你試圖重新發明輪子。使用常規的組合框。 (或從WPF工具包或其他東西的自動完成框) –

回答

0

我不知道確切的問題,但這裏是一個建議:

別子組合框 - 你不需要。

只需將一個放在MainForm上,然後從後面的代碼中使用它 - 使用它就會更容易。如果您需要更改組合框的工作方式,那麼您只需要繼承子類即可。

我試着下載測試項目,但它不是一個zip文件 - 它是一個.7z,我無法打開。

此外,你的主窗體是一個窗口。這可能不是一個問題,但嘗試創建一個新項目,並使用項目爲您提供的主窗體。有一些在App中完成的調用MainForm的接口可以捕獲異常等,這可能會給你更多的數據來解決問題。

格雷格