简体   繁体   English

将Listview从Winforms转换为WPF

[英]Convert Listview from Winforms to WPF

Hi I am trying to convert my Winforms application over to WPF to merge it with another WPF application. 嗨,我正在尝试将Winforms应用程序转换为WPF,以将其与另一个WPF应用程序合并。

The main part I am stuck on is linking the data to my ListView columns, in Winform I have: 我遇到的主要问题是将数据链接到ListView列,在Winform中,我有:

        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. 我似乎找不到一种将每个controllerInfo绑定到其对应列的方法。 This is my xaml code for my ListView1: 这是我的ListView1的xaml代码:

    <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>

Just a friendly advice, learn MVVM before writing a single line of code in wpf. 只是一个友好的建议,在用wpf编写一行代码之前,请学习MVVM To solve your problem here, you need not to touch Items property direcly on the ListView . 要在这里解决您的问题,您无需直接触摸ListView上的Items属性。 Just set the ItemsSource as below and it will work. 只需将ItemsSource设置如下,即可使用。

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

    listView1.ItemsSource = controllers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM