WPF系列:GridView列绑定控件(一)

    最近,在WPF方面的UI设计,设计很多控件的绑定和布局,因为刚开始学习,所以,有很多东西不是很懂,一边在网上搜,一边试着做,因为设计到在GridView中绑定控件,所有,在网上搜了搜,看了一篇很好的帖子,自己学着做了做,现在就总结一个WPF中如何在GridView中列中绑定其他的控件。为了增加效果,在WPF项目中用到了一个第三方的控件telerik,后面附加上相关的dll引用,在WPF项目中自己添加到自己的项目中就可以使用了。

   首先,贴出我绑定后的GirdView的效果图:

   

WPF系列:GridView列绑定控件(一)_第1张图片

由于采用了telerik:StyleManager.Theme="Windows7"风格,所有可能看起来不是很像GridView,下面就给出xaml代码:

  LstData, Mode=TwoWay}" SelectionMode="Extended" RowHeight="50"     telerik:StyleManager.Theme="Windows7">
                
                    
                    
                        
                            
                                Level,Mode=TwoWay}" 
VerticalAlignment="Center" Height="22">
                            
                        
                    

                    
                        
                            
                                
                            
                        
                    

                    
                        
                            
                                
                                    

                                    
                                    
                                
                            
                        

                    
                    
                        
                            
                                
                                    

                                    
                                
                            
                        

                    
                
            

  上面要添加telerik的引用 :xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"

这样,就可以在GridView的每一个列上加上想要的控件了,下面是后台的代码,由于是第一个版本,所有代码很多都没有完善,但是,基本的界面显示功能已经可以实现:

 private string[] _StructureData = { "致密", "较致密", "疏松", "较疏松", "性脆易碎", "其他" };
        private string[] _BaseColorData = { "亮黑色", "褐黑色", "黑色", "褐色", "其他" };
        LevelCollection pc = new LevelCollection();
        public CobaltRichCrustUIGroupBoxLevel()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            pc.LstData.Add(new LevelInfo() { Level = "I", Structure = _StructureData.ToList(), BaseColor = _BaseColorData.ToList() });
            pc.LstData.Add(new LevelInfo() { Level = "II", Structure = _StructureData.ToList(), BaseColor = _BaseColorData.ToList() });
            pc.LstData.Add(new LevelInfo() { Level = "III", Structure = _StructureData.ToList(), BaseColor = _BaseColorData.ToList() });
            pc.LstData.Add(new LevelInfo() { Level = "IV", Structure = _StructureData.ToList(), BaseColor = _BaseColorData.ToList() });
            pc.LstData.Add(new LevelInfo() { Level = "V", Structure = _StructureData.ToList(), BaseColor = _BaseColorData.ToList() });
            pc.LstData.Add(new LevelInfo() { Level = "VI", Structure = _StructureData.ToList(), BaseColor = _BaseColorData.ToList() });
            this.DataContext = pc;
        }
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnSub_Click(object sender, RoutedEventArgs e)
        {

        }


    }
    public class LevelCollection
    {
        private ObservableCollection lstData = new ObservableCollection();

        public ObservableCollection LstData
        {
            get { return lstData; }
            set { lstData = value; }
        }
    }

    public class LevelInfo : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        private string _Level = "";
        private List _Structure = new List();
        private List _BaseColor = new List();

        public List BaseColor
        {
            get { return _BaseColor; }
            set
            {
                _BaseColor = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("BaseColor"));
                }
            }
        }

        public List Structure
        {
            get { return _Structure; }
            set
            {
                _Structure = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Structure"));
                }
            }
        }

        public string Level
        {
            get { return _Level; }
            set
            {
                _Level = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Level"));
                }
            }
        }
    }

  上面的代码很多都是Copy网上的程序,只是经过稍微修改可以使用了,具体的很多命名都没有进行规范,后期版本在处理数据时,会进行命名规范。

Telerik相关的dll文件:Telerik.rar

转载于:https://www.cnblogs.com/lufangtao/archive/2012/08/10/2631788.html

你可能感兴趣的:(WPF系列:GridView列绑定控件(一))