如何将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;
}
}
}
答案 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