繁体   English   中英

WPF中的自动完成文本框

[英]Auto Complete Textbox in wpf

我正在尝试使用此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;

任何帮助将不胜感激

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

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();
            }
        }
    }
}
class MyViewModel {
    public IEnumerable<string> TestItems;
}

并在您的构造函数中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM