WPF List View 用法

效果:

WPF List View 用法_第1张图片


XAML:


                        
                            
                                
                                
                                
                                

                            
                        
                    

如何添加一行数据:

 假设我们的Listview控件名称是lvActionEditList

先定义一个结构体

struct mhAction
        {
            private string num;
            private string action;
            private string axis;
            private string pos;
            private string value;

            public mhAction(string v1, string v2, string v3, string v4, string v5) : this()
            {
                this.num = v1;
                this.action = v2;
                this.axis = v3;
                this.pos = v4;
                this.value = v5;
            }
        }

添加一条数据到ListView

 lvActionEditList.Items.Add(new mhAction("0","MOV","8,9,10,11","500,450,500,500","0"));

WPF List View 用法_第2张图片

会显示成这样是因为尚未将数据进行绑定


回到XAML编辑界面,根据结构体名称,绑定数据:


                        
                            
                                
                                
                                
                                
                                

                            
                        
                    

显示出来是空白的,因为我们必须要手动重写结构体声明的部分,加上{get;set;},还需要把结构体这边的私有改为公有

struct mhAction
        {
            public string num { get; set; }
            public string action { get; set; }
            public string axis { get; set; }
            public string pos { get; set; }
            public string value { get; set; }

            public mhAction(string v1, string v2, string v3, string v4, string v5) : this()
            {
                this.num = v1;
                this.action = v2;
                this.axis = v3;
                this.pos = v4;
                this.value = v5;
            }
        }


如果要居中显示数据,在XAML文件添加自定义风格的描述

 
        
    

应用在ListView上面,修改ListView描述

 

你可能感兴趣的:(C/C#,ListView,WPF)