如何在WPF中绑定到ListView [英] How to bind to ListView in WPF

查看:86
本文介绍了如何在WPF中绑定到ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我如何将listview绑定到可观察的集合,我尝试了有关此的许多示例,但是没有运气.

我有以下使用WMI收集系统信息的类

Hello Everyone

How can i bind listview to observable collection, i tried many example posted on Net regarding this but with no luck.

I have the following class which collects system information using WMI

<pre lang="c#">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Windows;
using System.Collections.ObjectModel;

namespace WMI_WPF
{
    public class SystemDetails
    {
        ManagementObjectQuery ManagementObj = new ManagementObjectQuery();

        

        public string ComputerName { get; set; }
        public string Manufacturer { get; set; }
        public string Version { get; set; }
        public string OSName { get; set; }
        public string BuildNumber { get; set; }
        //public string[] IP { get; set; }
        public string MACAddress { get; set; }
        public string UserName { get; set; }
        public string Domain { get; set; }


        public ObservableCollection<systemdetails> SystemDetail = new              
        ObservableCollection<systemdetails>();

        public SystemDetails()
        {
            
        }

        public ObservableCollection<systemdetails> FillSystemDetails()
        {
            SystemDetails _sysdet = new SystemDetails();
            ManagementObjectCollection ObjCol =    
            ManagementObj.QueryInfo("Win32_OperatingSystem");

            foreach (ManagementObject OS in ObjCol)
            {
               _sysdet.ComputerName = OS["csname"].ToString().Trim();
               _sysdet.Version = OS["Version"].ToString().Trim();
               _sysdet.Manufacturer = OS["Manufacturer"].ToString().Trim();
               _sysdet.OSName = OS["name"].ToString().Trim();
               _sysdet.BuildNumber = OS["BuildNumber"].ToString().Trim();
            }

            ManagementObjectCollection IpCol = 
            ManagementObj.QueryInfo("Win32_NetworkAdapterConfiguration WHERE 
            IPEnabled = ''TRUE''");
            foreach (ManagementObject IPaddr in IpCol)
            {
                
                _sysdet.MACAddress = (string)IPaddr["MACAddress"];
               
            }


            ManagementObjectCollection CurrentUser = 
            ManagementObj.QueryInfo("Win32_ComputerSystem");
            foreach (ManagementObject User in CurrentUser)
            {
                _sysdet.UserName = User["UserName"].ToString();
               
            }
           
            SystemDetail.Add(_sysdet);
            return SystemDetail;

        }

        public ObservableCollection<systemdetails> SysDet
        {
            get
            {
                return SystemDetail;
            }
        }
    }
    }</systemdetails></systemdetails></systemdetails></systemdetails>



我如何将此SystemDetail可观察的集合绑定到Listview

谢谢您.



How can i bind this SystemDetail observable collection to Listview

Thank you

推荐答案

您需要确保xaml绑定正确无误,并且DisplayMemberBinding ItemSource 都已设置.

这是一个很好的分步教程 [
You need to make sure your xaml bindings are all correct and DisplayMemberBinding and ItemSource have both been set.

Here is a good step by step tutorial[^] that should guide you.


这篇关于如何在WPF中绑定到ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆