如何将ListView中的选定项目添加到TextBox?

时间:2014-06-24 11:08:02

标签: c# wpf listview

如何将ListView中的选定项目添加到TextBox?

private void txtautosgn_TextChanged(object sender, TextChangedEventArgs e)
{
    if (txtautosgn.Text.Length > 1)
    {
        var names = (from autonames in lstDetails
                     where autonames.name.Contains(txtautosgn.Text.Trim())
                     select autonames.name).ToList();

        if (names.Count > 1)
        {
            lstnames.Items.Clear();
            lstnames.Visibility = Visibility.Visible;
            foreach (string name in names)
            {
                lstnames.Items.Add(name);
            }
            lstnames.SelectedIndex = 0;

        }
        else
        {
            lstnames.Visibility = Visibility.Collapsed;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在您的方法中,不要在ListView中添加项目,而是将ItemsSource设置为:

    var names = (from autonames in lstDetails
                 where autonames.name.Contains(txtautosgn.Text.Trim())
                 select autonames.name).ToList();

    if (names.Count > 1)
    {
        lstnames.Items.Clear();
        lstnames.Visibility = Visibility.Visible;
        lstnames.ItemsSource = names;
        lstnames.SelectedIndex = 0;

    }
    else
    {
        lstnames.Visibility = Visibility.Collapsed;
    }

在Xaml中,您可以将SelectedItem的{​​{1}}绑定到Listview

Textbox