将文本从文本框添加到ListView列

时间:2019-12-13 05:27:51

标签: c# .net wpf

我有2列的ListView。当我点击 ADD 按钮时,我希望第一行文本进入第一列,第二行文本进入第二列,但是它不起作用。

我正在使用Visual Studio 2003,c#WPF。

listView1.View = View.Details;
ListViewItem lvwItem = listView1.Items.Add("TextBox1.Text");
lvwItem.SubItems.Add("TextBox2.Text");

1 个答案:

答案 0 :(得分:-1)

您正在WPF项目中使用Winforms方法!

您可能想使用它!

https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/listview-overview

你想要什么!

回答您的问题https://stackoverflow.com/a/15866118/10505137

public struct NamePhone
{
    public NamePhone(string _name, string _phone)
    {
        name = _name;
        phone = _phone;
    }

    string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    string phone;
    public string Phone
    {
        get { return phone; }
        set { phone = value; }
    }
}


{
    GridView view = new GridView();

    GridViewColumn col1 = new GridViewColumn();
    col1.Header = "Name";
    col1.DisplayMemberBinding = new Binding("Name");
    view.Columns.Add(col1);

    GridViewColumn col2 = new GridViewColumn();
    col2.Header = "Phone";
    col2.DisplayMemberBinding = new Binding("Phone");
    view.Columns.Add(col2);

    listView1.View = view;

    listView1.Items.Add(new NamePhone("Nish", "777"));
    listView1.Items.Add(new NamePhone("Alex", "555"));
}