簡體   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