Adding items to a WPF ListView with columns

I use this XAML code for ListView:

<ListView> <ListView.View> <GridView> <GridViewColumn DisplayMemberBinding="{Binding Path=Flag}" /> <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" /> <GridViewColumn Header="Ip Address" DisplayMemberBinding="{Binding Path=IpAddress}" /> </GridView> </ListView.View> </ListView> 

And this is how I add items to the ListView:

 ServerListItem item = new ServerListItem { Flag = "IL", Name = "Sample Server", IpAddress = "sample-server.com" }; lvServerList.Items.Add(item); 

Here is the ServerListItem class:

 public class ServerListItem { public string Flag; public string Name; public string IpAddress; } 

The item is added to the ListView, but all columns are empty. What should I do?

+4
source share
1 answer

WPF does not bind to fields, but only to properties. Flag, Name, and IpAddress are defined as public fields in your class. Modify the class definition to use auto properties instead:

 public class ServerListItem { public string Flag { get; set; } public string Name { get; set; } public string IpAddress { get; set; } } 
+7
source

All Articles