Question

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>
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top