tags:

views:

55

answers:

1

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?

+2  A: 

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; }
} 
Adam Sills
Thank you it works :)
Zippo