0

So I am trying to add a row of information to my listview but when I do it displays it weirdly. Like so:

enter image description here

I am using an for each loop like so:

foreach (Client c in clients)
{
   ListViewItem i = new ListViewItem();
   i.Content = new String[] { c.info.cid.ToString(), c.info.pc.ToString(),c.info.ip.ToString(), c.info.status.ToString() };
   list.Items.Add(i);
}

My Client class is using a struct to store the info

public struct Info
{
  public int cid;
  public string pc;
  public string ip;
  public string status;
}

I am also adding values to it:

info = new Info();
info.ip = "192.168.1.100";
info.pc = "Duncan";
info.status = "idle";
info.cid = 1;

Why is it displaying it weirdly? Could anyone help?

My ListView XAML:

<ListView Height="247" HorizontalAlignment="Left" Margin="4,6,0,0" Name="list" VerticalAlignment="Top" Width="319" Background="#FF454545" ItemsSource="{Binding}" SelectionMode="Multiple" Grid.Column="0">
   <ListView.View>
       <GridView AllowsColumnReorder="False">
       <GridViewColumn Header="ID" Width="30" />
       <GridViewColumn Header="Computer" Width="100" />
       <GridViewColumn Header="IP" Width="100" />
       <GridViewColumn Header="Status" Width="100" />
       </GridView>
    </ListView.View>
    </ListView>
1
  • Just an advice, you don't need to use .ToString() on the variables which are of type string. Commented Oct 13, 2012 at 12:05

1 Answer 1

2

There are some wrong things in this code. If you want to push data in a ListView using bindings, you have to have a valid ViewModel with properties to bind on. You have to define the bindings on you GridViewColumns.

Moreover, WPF doesnt know how to bind on fields, so you will need .NET properties for each data you want to display. Here is a very raw example for your case, it's not a realistic scenario but should help you get started :

Window.xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<ListView Height="247" HorizontalAlignment="Left" Margin="4,6,0,0" Name="list" VerticalAlignment="Top" Width="319" Background="#FF454545" ItemsSource="{Binding Clients}" SelectionMode="Multiple" Grid.Column="0">
    <ListView.View>
        <GridView AllowsColumnReorder="False">
            <GridViewColumn Header="ID" Width="30" DisplayMemberBinding="{Binding Id}" />
            <GridViewColumn Header="Computer" Width="100" DisplayMemberBinding="{Binding Computer}" />
            <GridViewColumn Header="IP" Width="100" DisplayMemberBinding="{Binding Ip}" />
            <GridViewColumn Header="Status" Width="100" DisplayMemberBinding="{Binding Status}" />
        </GridView>
    </ListView.View>
</ListView>
</Window>

MainWindow.xaml.cs

/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;           
    }

    public IEnumerable<DummyClient> Clients
    {
        get
        {
            for (int i = 0; i < 10; i++)
            {
                var info = new Info();
                info.ip = "192.168.1.100";
                info.pc = "Duncan";
                info.status = "idle";
                info.cid = 1;

                yield return new DummyClient(info);
            }
        }
    }
}

public class DummyClient
{
    public DummyClient(Info info)
    {
        Info = info;
    }

    public string Ip { get { return Info.ip; } }
    public string Computer { get { return Info.pc; } }
    public string Status { get { return Info.status; } }
    public int Id { get { return Info.cid; } }

    public Info Info
    {
        get;
        private set;
    }
}

public struct Info
{
    public int cid;
    public string pc;
    public string ip;
    public string status;
}

Once again it's not really the way it should be done but this is a start. For exampe, DummyClient should implement INotifyPropertyChanged if you want two ways bindings to works.

Sign up to request clarification or add additional context in comments.

5 Comments

So how would I had the tiems to the list?
You bind to an object collection. You don't have to create ListViewItems manually, the ListView ItemsGenerator does that for you. In this case, I bound the ListView ItemsSource on a basic DummyClient collection. You can just dump this code in a new project and adapt it to your needs, it should work.
Sorry could you elaborate, I'm a bit confused.
Okay :) ListView's DataContext is the MainWindow. It's the object that will be used as a Source for bindings. This is ensured by the "DataContext = this;" statement in the window constructor (it's not a good practice by the way). Now in xaml, ListView's ItemsSource is set to {Binding Clients}. It's means the ListView will be populated with object in Clients collection in the MainWindow. So if you want to add items to the ListView you just have to add objects to this collection.
Finally, each column binds to a specific property of the DummyClient object : that is ensured by the GridViewColumn DisplayMemberBinding property in the xaml. Good luck :) EDIT: Glad I could help.

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.