0

Hi I am trying to convert my Winforms application over to WPF to merge it with another WPF application.

The main part I am stuck on is linking the data to my ListView columns, in Winform I have:

        listView1.Items.Clear();
        this.scanner.Scan();
        ControllerInfoCollection controllers = scanner.Controllers;
        ListViewItem item = null;
        foreach (ControllerInfo controllerInfo in controllers)
        {
            item = new ListViewItem(controllerInfo.IPAddress.ToString());
            item.SubItems.Add(controllerInfo.Availability.ToString());
            item.SubItems.Add(controllerInfo.IsVirtual.ToString());
            item.SubItems.Add(controllerInfo.SystemName);
            item.SubItems.Add(controllerInfo.Version.ToString());
            item.SubItems.Add(controllerInfo.ControllerName);
            this.listView1.Items.Add(item);
            item.Tag = controllerInfo;
        }
    }

I cant seem to find a way to bind each of the controllerInfo pieces to its corresponding column. This is my xaml code for my ListView1:

    <ListView.View>
    <GridView AllowsColumnReorder="True">
    <GridViewColumn DisplayMemberBinding="{Binding Path=IPAdress}" Header="IP" Width="65"/>
    <GridViewColumn DisplayMemberBinding="{Binding Path=Availability}" Header="Availability" Width="60"/>
    <GridViewColumn DisplayMemberBinding="{Binding Path=IsVirtual}" Header="Virtual" Width="40"/>
    <GridViewColumn DisplayMemberBinding="{Binding Path=SystemName}" Header="System name" Width="75"/>
    <GridViewColumn DisplayMemberBinding="{Binding Path=Version}" Header="RobotWare" Width="60"/>
    <GridViewColumn DisplayMemberBinding="{Binding Path=ControllerName}" Header="Controller Name" Width="100"/>
    </GridView>
    </ListView.View>
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • 1
    Did you mention the source in the listview element? Also make sure you bind to an ObsevableCollection – dburner May 14 '14 at 10:25

1 Answers1

0

Just a friendly advice, learn MVVM before writing a single line of code in wpf. To solve your problem here, you need not to touch Items property direcly on the ListView. Just set the ItemsSource as below and it will work.

    this.scanner.Scan();
    ControllerInfoCollection  controllers = scanner.Controllers;

    listView1.ItemsSource = controllers
Nitin
  • 18,344
  • 2
  • 36
  • 53
  • Thank you! I am nearing a project deadline and need to convert this winforms to wpf. I will try to learn MVVM but time is of the essence. – MechatronicsStudent May 14 '14 at 11:47
  • I am now trying to convert my selected item to a listviewitem `ListViewItem item = this.listView1.SelectedItems[0]` I cannot find the correct explicit conversion to use or is my naivety doing me in again? – MechatronicsStudent May 14 '14 at 11:48
  • why do you need a listviewitem? – Nitin May 14 '14 at 13:25
  • I'm just trying to convert it and that line had an error. The item is used in the rest of the event handler where if item!=null some things happen depending on its properties – MechatronicsStudent May 14 '14 at 13:43
  • If your listview is not multiselect then `ControllerInfo controller = listView1.SelectedItem as ControllerInfo` will give you selected controller... for multiselect `ControllerInfo controller = listView1.SelectedItems[0] as ControllerInfo` will give you first selected controller. There is no need for ListViewItem – Nitin May 14 '14 at 13:51
  • http://stackoverflow.com/questions/23654281/convert-object-to-listviewitem?noredirect=1#comment36334610_23654281 – MechatronicsStudent May 14 '14 at 14:35