0

次のようなオートコンプリート TextBox があります。

public class AutoCompleteTextBox:ComboBox
{
    public AutoCompleteTextBox()
    {
        ResourceDictionary rd = new ResourceDictionary();
        rd.Source = new Uri("/"+this.GetType().Assembly.GetName().Name+";component/Styles/MainViewStyle.xaml",UriKind.Relative);
        this.Resources = rd;
        this.IsTextSearchEnabled = false;
    }

    /// <summary>
    /// Override OnApplyTemplate method 
    /// Get TextBox control out of Combobox control, and hook up TextChanged event.
    /// </summary>
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        //get the textbox control in the ComboBox control
        TextBox textBox = this.Template.FindName("PART_EditableTextBox", this) as TextBox;
        if (textBox != null)
        {
            //disable Autoword selection of the TextBox
            textBox.AutoWordSelection = false;
            //handle TextChanged event to dynamically add Combobox items.
            textBox.TextChanged += new TextChangedEventHandler(textBox_TextChanged);                
        }
    }       


    public ObservableCollection<string> SuggestionList
    {
        get { return (ObservableCollection<string>)GetValue(SuggestionListProperty); }
        set { SetValue(SuggestionListProperty, value); }
    }



    public static readonly DependencyProperty SuggestionListProperty = DependencyProperty.Register("SuggestionList", typeof(ObservableCollection<string>), typeof(AutoCompleteTextBox), new UIPropertyMetadata());


    /// <summary>
    /// main logic to generate auto suggestion list.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Windows.Controls.TextChangedEventArgs"/> 
    /// instance containing the event data.</param>
    void textBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox textBox = sender as TextBox;
        textBox.AutoWordSelection = false;
        // if the word in the textbox is selected, then don't change item collection
        if ((textBox.SelectionStart != 0 || textBox.Text.Length == 0))
        {
            this.Items.Clear();
            //add new filtered items according the current TextBox input
            if (!string.IsNullOrEmpty(textBox.Text))
            {
                foreach (string s in this.SuggestionList)
                {
                    if (s.StartsWith(textBox.Text, StringComparison.InvariantCultureIgnoreCase))
                    {

                        string unboldpart = s.Substring(textBox.Text.Length);
                        string boldpart = s.Substring(0, textBox.Text.Length);
                        //construct AutoCompleteEntry and add to the ComboBox
                        AutoCompleteEntry entry = new AutoCompleteEntry(s, boldpart, unboldpart);
                        this.Items.Add(entry);
                    }
                }
            }
        }
        // open or close dropdown of the ComboBox according to whether there are items in the 
        // fitlered result.
        this.IsDropDownOpen = this.HasItems;

        //avoid auto selection
        textBox.Focus();
        textBox.SelectionStart = textBox.Text.Length;

    }
}

/// <summary>
/// Extended ComboBox Item
/// </summary>
public class AutoCompleteEntry : ComboBoxItem
{
    private TextBlock tbEntry;

    //text of the item
    private string text;

    /// <summary>
    /// Contrutor of AutoCompleteEntry class
    /// </summary>
    /// <param name="text">All the Text of the item </param>
    /// <param name="bold">The already entered part of the Text</param>
    /// <param name="unbold">The remained part of the Text</param>
    public AutoCompleteEntry(string text, string bold, string unbold)
    {
        this.text = text;
        tbEntry = new TextBlock();
        //highlight the current input Text
        tbEntry.Inlines.Add(new Run
        {
            Text = bold,
            FontWeight = FontWeights.Bold,
            Foreground = new SolidColorBrush(Colors.RoyalBlue)
        });
        tbEntry.Inlines.Add(new Run { Text = unbold });
        this.Content = tbEntry;
    }

    /// <summary>
    /// Gets the text.
    /// </summary>
    public string Text
    {
        get { return this.text; }
    }
}

そして私のxamlは次のようになります:

<local:AutoCompleteTextBox SuggestionList="{Binding Suggestions}" 
                                           Text="{Binding Path=Keyword,UpdateSourceTrigger=PropertyChanged}"
                                           x:Name="SearchTextBox"/>

Enterボタンを押すと検索コマンドを実行したいのですが、画面に提案コンボボックスが表示されている場合、Enterボタンを押して提案コンボボックスを閉じるだけで、もう一度Enterボタンを押してコマンドを実行する必要があります。提案コンボボックスを閉じて、Enterボタンを1回押してコマンドを実行する方法はありますか?

ありがとう

4

1 に答える 1

0

DropDownClosedイベントの処理時に検索を呼び出すことができます。

<local:AutoCompleteTextBox SuggestionList="{Binding Suggestions}" 
                                           Text="{Binding Path=Keyword,UpdateSourceTrigger=PropertyChanged}"
                                           DropDownClosed="OnDropDownClosed"
                                           x:Name="SearchTextBox"/>

そして OnDropDownClosed:

private void OnDropDownClosedobject sender, RoutedPropertyChangedEventArgs<bool> e)
{
    // search on Keyword
}
于 2013-11-07T04:01:41.253 に答える