ryan
ryan

Reputation: 95

Text content in listview not displaying C#

I have a listview control and the text will not show up. I have tried 3 different implementations and I get the same result in each. The rows for the associated number of items will highlight, but the text is nowhere. I am using WPF and the text contents look like the following in the file I am reading in:

string1::string2

However, for testing purposes, I am not utilizing the file contents.

Tutorial I'm Using: http://www.wpf-tutorial.com/listview-control/listview-with-gridview/

"1" and "2" were in there just to test.

enter image description here

Code

        List<ServerData> items = new List<ServerData>();

        while ((line = file.ReadLine()) != null )
        {
            string[] serverData = Regex.Split(line, "::");
            items.Add(new ServerData() { IPAddress="1", ServerName="2" });
        }
        listServer.ItemsSource = items;
        file.Close();

....

public class ServerData
{
    public string IPAddress { get; set; }
    public string ServerName { get; set; }
}

XAML

<ListView Margin="10,47,11,45.8" x:Name="listServer" Grid.RowSpan="2">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="IP Address" Width="218.4" DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Header="Server Name" Width="218.4" DisplayMemberBinding="{Binding Name}" />
        </GridView>
    </ListView.View>
</ListView>

Upvotes: 1

Views: 365

Answers (1)

Clint
Clint

Reputation: 6220

<ListView Margin="10,47,11,45.8" x:Name="listServer" Grid.RowSpan="2">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="IP Address" Width="218.4" DisplayMemberBinding="{Binding Name}" />
            <GridViewColumn Header="Server Name" Width="218.4" DisplayMemberBinding="{Binding Name}" />
        </GridView>
    </ListView.View>
</ListView>

Here's your problem, you're binding the columns to Name, when instead it should be:

<ListView Margin="10,47,11,45.8" x:Name="listServer" Grid.RowSpan="2">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="IP Address" Width="218.4" DisplayMemberBinding="{Binding IPAddress}" />
            <GridViewColumn Header="Server Name" Width="218.4" DisplayMemberBinding="{Binding ServerName}" />
        </GridView>
    </ListView.View>
</ListView>

Upvotes: 1

Related Questions