C# wpf中的自动完成文本框

C# wpf中的自动完成文本框,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,我正在尝试使用此链接中提到的方法制作一个自动完成的文本框。但我无法使其工作 我的XAML代码如下: <TextBox Name="searchBox" Width="250" HorizontalAlignment="Center" Text="{Binding TestText, UpdateSourceTrigger=PropertyChanged}" behaviors:AutoComp

我正在尝试使用此链接中提到的方法制作一个自动完成的文本框。但我无法使其工作

我的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;
IEnumerable TestItems=new List(){“John”、“Mark”、“Doe”};
this.DataContext=TestText;
任何帮助都将不胜感激

class MyViewModel{
class MyViewModel {
    public IEnumerable<string> TestItems;
}
公众可数睾丸; }
在您的构造函数中:

IEnumerable<string> TestItems = new List<string>() { "John", "Mark","Doe" };
this.DataContext = new MyViewModel { TestItems = TestItems };
IEnumerable TestItems=new List(){“John”、“Mark”、“Doe”};
this.DataContext=newmyviewmodel{TestItems=TestItems};
分类MyViewModel{
公众可数睾丸;
}
在您的构造函数中:

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

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

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();
            }
        }
    }
}
公共部分类主窗口:窗口
{
收集清单;
公共主窗口()
{
初始化组件();
stringCollection=新列表
{
“abc”、“ayr”、“bef”、“bcs”、“caa”、“lmn”
};
txtAuto.TextChanged+=txtAuto\u TextChanged;
}
void txtAuto_TextChanged(对象发送方,TextChangedEventArgs e)
{
字符串类型字符串=txtAuto.Text;
List autoList=新列表();
autoList.Clear();
foreach(stringCollection中的字符串项)
{
如果(!string.IsNullOrEmpty(txtAuto.Text))
{
if(项目包含(类型字符串))
{
自动列表。添加(项目);
}
}
}
如果(autoList.Count>0)
{
lblSuggestion.ItemsSource=自动列表;
lblSuggestion.Visibility=可见性.Visibility;
}
else if(txtAuto.Text.Equals(“”)
{
lblSuggestion.Visibility=可见性。已折叠;
lblSuggestion.ItemsSource=null;
}
其他的
{
lblSuggestion.Visibility=可见性。已折叠;
lblSuggestion.ItemsSource=null;
}
}
void lbl建议\u selection已更改(对象发送者,selectionchangedventargs e)
{
if(lblSuggestion.ItemsSource!=null)
{
lblSuggestion.KeyDown+=lblSuggestion\u KeyDown;
}
}
私有void txtAuto_KeyDown(对象发送方,KeyEventArgs e)
{
如果(e.Key==Key.Down)
{
lblSuggestion.Focus();
}
}
私有void lblssuggestion_KeyDown(对象发送方,KeyEventArgs e)
{
if(ReferenceEquals(发送方,lbl建议))
{
如果(e.Key==Key.Enter)
{
txtAuto.Text=lblSuggestion.SelectedItem.ToString();
lblSuggestion.Visibility=可见性。已折叠;
}
如果(e.Key==Key.Down)
{
e、 已处理=正确;
lblSuggestion.Items.MoveCurrentToNext();
}
如果(e.Key==Key.Up)
{
e、 已处理=正确;
lblSuggestion.Items.MoveCurrentToPrevious();
}
}
}
}

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

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();
            }
        }
    }
}
公共部分类主窗口:窗口
{
收集清单;
公共主窗口()
{
初始化组件();
stringCollection=新列表
{
“abc”、“ayr”、“bef”、“bcs”、“caa”、“lmn”
};
txtAuto.TextChanged+=txtAuto\u TextChanged;
}
void txtAuto_TextChanged(对象发送方,TextChangedEventArgs e)
{
字符串类型字符串=txtAuto.Text;
List autoList=新列表();
autoList.Clear();
foreach(stringCollection中的字符串项)
{
如果(!string.IsNullOrEmpty(txtAuto.Text))
{
if(项目包含(类型字符串))
{
自动列表。添加(项目);
}
}
}
如果(autoList.Count>0)
{
lblSuggestion.ItemsSource=自动列表;
lblSuggestion.Visibility=可见性.Visibility;
}
else if(txtAuto.Text.Equals(“”)
{
lblSuggestion.Visibility=可见性。已折叠;
lblSuggestion.ItemsSource=null;
}
其他的
{
lblSuggestion.Visibility=可见性。已折叠;
lblSuggestion.ItemsSource=null;
}
}
void lbl建议\u selection已更改(对象发送者,selectionchangedventargs e)
{
if(lblSuggestion.ItemsSource!=null)
{
lblSuggestion.KeyDown+=lblSuggestion\u KeyDown;
}
}
私有void txtAuto_KeyDown(对象发送方,KeyEventArgs e)
{
如果(e.Key==Key.Down)
{
lblSuggestion.Focus();
}
}
私有void lblssuggestion_KeyDown(对象发送方,KeyEventArgs e)
{
if(ReferenceEquals(发送方,lbl建议))
{
如果(e.Key==Key.Enter)
{
txtAuto.Text=lblSuggestion.SelectedItem.ToString();
lblSuggestion.Visibility=可见性。已折叠;
}
如果(e.Key==Key.Down)
{
e、 已处理=正确;
lblSuggestion.Items.MoveCurrentToNext();
}
如果(e.Key==Key.Up)
{
e、 已处理=正确;
lblSuggestion.Items.MoveCurrentToPrevious();
}
}
}
}

我试图以此为例。您的示例仅在文本框为空时显示列表。我想标记人,所以当出现分号时,它应该标记这个词。我已经完成了标记工作。但是我在这里如何做呢?在上面的示例中,它显示了列表