c#wpf将列表绑定到Listview - listview行为空[关闭]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#wpf将列表绑定到Listview - listview行为空[关闭]相关的知识,希望对你有一定的参考价值。

我有一个问题,将数据从List<>绑定到WPF中的ListviewListView行为空,但列表值在List<>中设置。如果我使用ListBox,我会看到一行中的值。

查看列表的代码:

 public static List<string> ShowUSB()
    {
        var usbDevices = GetUSBDevices();
        List<string> Devices = new List<string>();
        foreach (var usbDevice in usbDevices)
        {
            string[] DeviceIDs = usbDevice.DevicePNPDeviceID.Split(new Char[] { '\', '&', '_' });
            usbDevice.DeviceVID = DeviceIDs[2];
            usbDevice.DevicePID = DeviceIDs[4];

            Devices.Add(usbDevice.DeviceManufacturer + usbDevice.DeviceName + usbDevice.DeviceVID + usbDevice.DevicePID); 

        }

        return Devices;
    }

这是用于绑定的XAML代码:

   <GroupBox Header="available Devices" Margin="1,223,1,75"  SnapsToDevicePixels="True" Grid.ColumnSpan="2">
                    <ListView x:Name="lvAddedDevices" Margin="0,0,0,0" >
                        <ListView.View>
                            <GridView>
                                <GridViewColumn x:Name="colManufacturer" Width="240" Header="Manufacturer" DisplayMemberBinding="{Binding DeviceManufacturer}"/>
                                <GridViewColumn x:Name="colDeviceName" Width="240" Header="Device Name" DisplayMemberBinding="{Binding DeviceName}"/>
                                <GridViewColumn x:Name="colPNPVID" Width="200" Header="VID ID" DisplayMemberBinding="{Binding DeviceVID}"/>
                                <GridViewColumn x:Name="colPNPPID" Width="200" Header="PID ID" DisplayMemberBinding="{Binding DevicePID}"/>
                                <!--<GridViewColumn x:Name="colDeviceStatus" Width="200" Header="Device Status"/>-->
                            </GridView>
                        </ListView.View>
                    </ListView>

                </GroupBox>

并在后面的代码中绑定调用:

   internal void LoadDevicesforList()
    {

        lvAddedDevices.ItemsSource = USBHandler.ShowUSB();
    }

这是完整的课程:

public class USBHandler
{

    public static List<string> ShowUSB()
    {
        var usbDevices = GetUSBDevices();
        List<string> Devices = new List<string>();
        foreach (var usbDevice in usbDevices)
        {
            string[] DeviceIDs = usbDevice.DevicePNPDeviceID.Split(new Char[] { '\', '&', '_' });
            usbDevice.DeviceVID = DeviceIDs[2];
            usbDevice.DevicePID = DeviceIDs[4];

            Devices.Add(usbDevice.DeviceManufacturer + usbDevice.DeviceName + usbDevice.DeviceVID + usbDevice.DevicePID); 

        }

        return Devices;
    }

    static List<USBDeviceInfo> GetUSBDevices()
    {
        List<USBDeviceInfo> devices = new List<USBDeviceInfo>();

        ManagementObjectCollection collection;
        using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PnpEntity where Manufacturer Like 'ATMEL%' or Manufacturer Like 'Microchip%' or Manufacturer Like 'Texas%' "))
            collection = searcher.Get();
        if (collection.Count > 0)
        {
            foreach (var device in collection)
            {

                devices.Add(new USBDeviceInfo(
                (string)device.GetPropertyValue("Manufacturer"),
                (string)device.GetPropertyValue("Name"),
                (string)device.GetPropertyValue("PNPDeviceID")
                ));

            }
        }

        collection.Dispose();
        return devices;
    }
}

class USBDeviceInfo
{
    public USBDeviceInfo(string deviceManufacturer, string deviceName, string devicePNPDeviceID)
    {
        DeviceManufacturer = deviceManufacturer;
        DeviceName = deviceName;
        DevicePNPDeviceID = devicePNPDeviceID;
    }
    public string DeviceManufacturer { get; private set; }
    public string DeviceName { get; private set; }
    public string DevicePNPDeviceID { get; private set; }
    public string DeviceVID { get; set; }
    public string DevicePID { get; set; }
}
答案

ShowUSB方法更改为:

public static List<USBDeviceInfo> ShowUSB()
{
    var devices = GetUSBDevices();

    foreach (var device in devices)
    {
        var deviceIDs = usbDevice.DevicePNPDeviceID.Split('\', '&', '_');
        device.DeviceVID = deviceIDs[2];
        device.DevicePID = deviceIDs[4];
    }

    return devices;
}

或者甚至更好,删除整个方法并将USBDeviceInfo构造函数更改为:

public USBDeviceInfo(
    string deviceManufacturer, string deviceName, string devicePNPDeviceID)
{
    DeviceManufacturer = deviceManufacturer;
    DeviceName = deviceName;
    DevicePNPDeviceID = devicePNPDeviceID;

    var deviceIDs = devicePNPDeviceID.Split('\', '&', '_');
    DeviceVID = deviceIDs[2];
    DevicePID = deviceIDs[4];
}

然后将GetUSBDevices方法公开并直接调用

lvAddedDevices.ItemsSource = USBHandler.GetUSBDevices();

以上是关于c#wpf将列表绑定到Listview - listview行为空[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

将 wpf listview 绑定到数据集......可能......?

如何将控件绑定到 WPF 列表视图列

WPF-将列表绑定到列表视图

字典绑定到 ListView WPF

如何有条件地更改数据绑定 WPF ListView 的 CellTemplate 中控件的属性?

WPF ListView 中的多维绑定