4

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?

1 Answer 1

7

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; }
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.