wpf中的自动完成文本框

时间:2014-12-20 17:40:58

标签: c# wpf wpf-controls

我正在尝试使用此https://github.com/Nimgoble/WPFTextBoxAutoComplete/链接中提到的方法制作自动完成文本框。但我无法使其正常工作。

我的XAML代码如下:

<TextBox Name="searchBox"
            Width="250"
            HorizontalAlignment="Center"
            Text="{Binding TestText, UpdateSourceTrigger=PropertyChanged}" 
            behaviors:AutoCompleteBehavior.AutoCompleteItemsSource="{Binding TestItems}" 
             Margin="0,0,0,75" RenderTransformOrigin="0.86,0.706" 
        />

这是我的代码背后代码:

 IEnumerable<string> TestItems = new List<string>() { "John", "Mark","Doe" };
 this.DataContext= TestText;

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:3)

请尝试以下代码并自定义您喜欢的方式

xaml代码 -

 <Grid>
    <TextBox x:Name="txtAuto" HorizontalAlignment="Left" Height="38" Margin="181,87,0,0" PreviewKeyDown="txtAuto_KeyDown" VerticalAlignment="Top" Width="190"/> 
    <ListBox x:Name="lblSuggestion" HorizontalAlignment="Left" Height="60" VerticalAlignment="Top" Width="190" Margin="181,130,0,0" 
             Visibility="Collapsed" KeyDown="lblSuggestion_KeyDown"  SelectionChanged="lblSuggestion_SelectionChanged"/>

</Grid>

背后的代码 -

public partial class MainWindow : Window
{

    List<string> stringCollection;
    public MainWindow()
    {
        InitializeComponent();
        stringCollection = new List<string>
        {
            "abc","ayr","bef","bcs","caa","lmn"

        };

        txtAuto.TextChanged += txtAuto_TextChanged;
    }

    void txtAuto_TextChanged(object sender, TextChangedEventArgs e)
    {
        string typedString = txtAuto.Text;

        List<string> autoList = new List<string>();

        autoList.Clear();

        foreach (string item in stringCollection)
        {
            if (!string.IsNullOrEmpty(txtAuto.Text))
            {

                if (item.Contains(typedString))
                {
                    autoList.Add(item);
                }
            }
        }

        if (autoList.Count > 0)
        {
            lblSuggestion.ItemsSource = autoList;
            lblSuggestion.Visibility = Visibility.Visible;
        }
        else if (txtAuto.Text.Equals(""))
        {
            lblSuggestion.Visibility = Visibility.Collapsed;
            lblSuggestion.ItemsSource = null;
        }
        else
        {
            lblSuggestion.Visibility = Visibility.Collapsed;
            lblSuggestion.ItemsSource = null;
        }
    }

    void lblSuggestion_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (lblSuggestion.ItemsSource != null)
        {
            lblSuggestion.KeyDown += lblSuggestion_KeyDown;               
        }
    }

    private void txtAuto_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Down)
        {
            lblSuggestion.Focus();
        }

    }

    private void lblSuggestion_KeyDown(object sender, KeyEventArgs e)
    {
        if (ReferenceEquals(sender, lblSuggestion))
        {
            if (e.Key == Key.Enter)
            {
                txtAuto.Text = lblSuggestion.SelectedItem.ToString();
                lblSuggestion.Visibility = Visibility.Collapsed;
            }

            if (e.Key == Key.Down)
            {
                e.Handled = true;
                lblSuggestion.Items.MoveCurrentToNext();
            }
            if (e.Key == Key.Up)
            {
                e.Handled = true;
                lblSuggestion.Items.MoveCurrentToPrevious();
            }
        }
    }
}

答案 1 :(得分:1)

class MyViewModel {
    public IEnumerable<string> TestItems;
}

并在你的构造函数中:

IEnumerable<string> TestItems = new List<string>() { "John", "Mark","Doe" };
this.DataContext = new MyViewModel { TestItems = TestItems };