Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将ListView中的选定项设置为文本框?_C#_Wpf_Listview - Fatal编程技术网

C# 如何将ListView中的选定项设置为文本框?

C# 如何将ListView中的选定项设置为文本框?,c#,wpf,listview,C#,Wpf,Listview,如何将ListView中的选定项设置为文本框 private void txtautosgn_TextChanged(object sender, TextChangedEventArgs e) { if (txtautosgn.Text.Length > 1) { var names = (from autonames in lstDetails where autonames.name.Contains(txta

如何将ListView中的选定项设置为文本框

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;
        }
    }
}

在您的方法中,不要在
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中,您可以将
Listview
SelectedItem
绑定到
Textbox
as

 <Grid>
    <TextBox Text="{Binding SelectedItem, ElementName=lstnames}"></TextBox>
    <ListView x:Name="lstnames"></ListView>
 </Grid>