Adding items in WPF ListView with columns
I use this XAML code for the 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?
WPF does not bind to fields, only to properties. Flag, Name and IpAddress are defined as public fields on your class. Change 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; }
}
精彩评论