I've been struggling for a while now to add items to 2 columns in a ListView
. In my Windows Forms application I had a something like this:
// In my class library:
public void AddItems(ListView listView)
{
var item = new ListViewItem {Text = "Some Text for Column 1"};
item.SubItems.Add("Some Text for Column 2");
listView.Items.Add(item);
}
I would then call this class from my Form.cs
.
How can I do this in WPF? Preferably, I wouldn't like to use a lot of XAML.
Solution With Less XAML and More C#
If you define the
ListView
in XAML:Then you can add columns and populate it in C#:
See definition of
MyItem
below.Solution With More XAML and less C#
However, it's easier to define the columns in XAML (inside the
ListView
definition):And then just populate the list in C#:
See definition of
MyItem
below.MyItem
DefinitionMyItem
is defined like this: