wpf 我得到了“System.Collections.Generic.List”而不是数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13865712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:37:08  来源:igfitidea点击:

I got "System.Collections.Generic.List" instead of data

c#wpfc#-4.0wpf-controls

提问by Love_Egypt

I tried to make an auto-complete TextBoxlike Google Search with C# in a WPF application, basically what I want to do is have an auto-complete TextBoxwhich is bound to a SQL database table. the table has 2 fields(Barcode and Name),my code as below:

我试图TextBox在 WPF 应用程序中使用 C# 进行像 Google Search 这样的自动完成,基本上我想做的是有一个TextBox绑定到 SQL 数据库表的自动完成。该表有 2 个字段(条形码和名称),我的代码如下:

In XAML :

在 XAML 中:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="37*" />
        <RowDefinition Height="88*" />
    </Grid.RowDefinitions>
    <TextBlock Text="Type Your Search :" HorizontalAlignment="Left"            VerticalAlignment="Bottom" Width="112" Height="15.96" Margin="31,0,0,4" />

    <TextBox HorizontalAlignment="Right" VerticalAlignment="Bottom" Height="25" Width="325" Margin="0,0,10,0" x:Name="txtCAuto" TextWrapping="NoWrap" />

    <ListBox x:Name="lbSuggestion" SelectionChanged="lbSuggestion_SelectionChanged" Background="LightYellow" Grid.Row="1" Visibility="Collapsed" HorizontalAlignment="Right" VerticalAlignment="Top" Width="325" Margin="0,0,10,0"/>
</Grid>

Code behind:

后面的代码:

    List<string> nameList;
    List<Product> prodList;

    public List<string> SelProd4Sale(string str )
    {
        string constr = "Data Source=.;Initial Catalog=AgamistaStore;User ID=emad2012;Password=emad_2012";
        SqlConnection SqlCon = new SqlConnection(constr);
        SqlCommand SqlCmdProds = new SqlCommand();
        SqlCmdProds.Connection = SqlCon;
        SqlCmdProds.CommandType = CommandType.Text;
        SqlCmdProds.CommandText = "SELECT dbo.ProductsTbl.ProductID,ProductsTbl.ProductBarcode," + 
            "dbo.ProductsTbl.ProductName, dbo.ProductsTbl.SalePrice FROM dbo.ProductsTbl ";
        SqlCon.Open();
        SqlDataAdapter dapProds = new SqlDataAdapter();
        dapProds.SelectCommand = SqlCmdProds;
        DataSet dsProds = new DataSet();
        dapProds.Fill(dsProds);
        SqlCon.Close();
        prodList = new List<Product>();
        for (int i = 0; i < dsProds.Tables[0].Rows.Count; i++)
        {
            prodList.Add(new Product
                            (dsProds.Tables[0].Rows[i]["ProductBarcode"].ToString(),
                            dsProds.Tables[0].Rows[i]["ProductName"].ToString());
        }
        dsProds = null;

        nameList = new List<string>() 
        {
           prodList.ToString()
        };

        return nameList;
    }

    public Window2()
    {
        InitializeComponent();
        SelProd4Sale(txtCAuto.Text);
        txtCAuto.TextChanged += new TextChangedEventHandler(txtAuto_TextChanged);
    }

    #region TextBox-TextChanged-txtAuto
    private void txtAuto_TextChanged(object sender, TextChangedEventArgs e)
    {
        string typedString = txtCAuto.Text.ToUpper();
        List<string> autoList = new List<string>();
        autoList.Clear();

        foreach (string item in nameList)
        {
            if (!string.IsNullOrEmpty(txtCAuto.Text))
            {
                if (item.StartsWith(typedString))
                {
                    autoList.Add(item);
                }
            }
        }

        if (autoList.Count > 0)
        {
            lbSuggestion.ItemsSource = autoList;
            lbSuggestion.Visibility = Visibility.Visible;
        }
        else if (txtCAuto.Text.Equals(""))
        {
            lbSuggestion.Visibility = Visibility.Collapsed;
            lbSuggestion.ItemsSource = null;
        }
        else
        {
            lbSuggestion.Visibility = Visibility.Collapsed;
            lbSuggestion.ItemsSource = null;
        }
    }
    #endregion

    #region ListBox-SelectionChanged-lbSuggestion
    private void lbSuggestion_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (lbSuggestion.ItemsSource != null)
        {
            lbSuggestion.Visibility = Visibility.Collapsed;
            txtCAuto.TextChanged -= new TextChangedEventHandler(txtAuto_TextChanged);
            if (lbSuggestion.SelectedIndex != -1)
            {
                txtCAuto.Text = lbSuggestion.SelectedItem.ToString();
            }
            txtCAuto.TextChanged += new TextChangedEventHandler(txtAuto_TextChanged);
        }
    }
    #endregion
}

class Product
{
    private string _ProductBarcode = "";
    private string _ProductName = "";

    public Product(string prodName,string prodBarcode)
    {
        this._ProductBarcode = prodBarcode;
        this._ProductName = prodName;
    }

    public string ProductBarcode
    {
        get { return _ProductBarcode; }
        set { _ProductBarcode = value; }
    }

    public string ProductName
    {
        get { return _ProductName; }
        set { _ProductName = value; }
    }

}

When I run this I got "System.Collections.Generic.List" as result instead of data.

当我运行它时,我得到了“System.Collections.Generic.List”而不是数据。

Can somebody help me please & tell me what's wrong?

有人可以帮助我并告诉我出了什么问题吗?

回答by tukaef

The problem is in this code:

问题出在这段代码中:

nameList = new List<string>() 
        {
           prodList.ToString()
        };

Since ToString()it is not overriden in List<T>it just returns a class name (basic implementation from System.Object). As result, your list contains single entry "System.Collection.Generic.List". To apply ToString()to elements of a list and create new list, replace that code with:

因为ToString()它没有被覆盖,List<T>所以它只返回一个类名(来自 的基本实现System.Object)。结果,您的列表包含单个条目"System.Collection.Generic.List"。要应用于ToString()列表的元素并创建新列表,请将该代码替换为:

nameList = productList.Select(p => p.ToString()).ToList();