DevExpress的GridControl控件绑定动态数据时列排序问题解决

阅读更多
GridControl是一个表格组件,可以格式化显示结构数据。其基本使用方法如下:

    
        
        
        
        
    
    
        
    

   在上述例子中,GridControl的列在xaml文件里已经设定,顺序是排好的。当GridControl的控件在cs文件定义,通过binding进行绑定的时候,序列就会按照对象属性定义的先后进行排列了。这就带来一个问题。比如基类A定义了两个属性A1和A2,继承基类A的类B定义了两个属性B1和B2。当我们将B的实例绑定到视图中的GridControl时,显示的顺序是A1,A2,B1,B2。而我们希望的顺序是B1,A1,B2,A2,这个问题怎么处理呢?
    问题解决主要包含以下步骤:
       1)定义两个类:A和B
       2)将类B的实例与GridControl绑定
       3)定义GridControl列排序xml文件
       4)读取上述xml文件,将该排序定义绑定到GridControl
    具体过程:
       1)定义两个类:A和B
       
namespace com.test{
          public class A 
          {
           public string A1{get;set;}
           public string A2{get;set;}
          }
        }
        namespace com.test{
          public class B:A
          {
           public string B1{get;set;}
           public string B2{get;set;}
          }
        }

        2)将类B的实例与GridControl绑定
        viewmodel(Bviewmodel.cs)如下:
       
namespace com.test{
          public class Bviewmodel: INotifyPropertyChanged
          {
              //应该在构造函数为该集合赋一些测试数据
              private ObservableCollection b_vm= new ObservableCollection();
           }
          public ObservableCollection B_vm
          {
            get { return this.b_vm; }
            set { this.b_vm= value; }
          }
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if (propertyChanged != null)
            {
                propertyChanged(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        }

        xaml(BModel.xaml)文件定义如下:
       

    
        
    
    
    
      
            
                
            
        
    

        3)定义GridControl列排序xml文件
        order.xml:
      
 

  
    
      
        
          
            
			  
			  

			  
            
          
        
      
    
  

       4)读取上述xml文件,将该排序定义绑定到GridControl
          定义读取xml通用函数:
namespace com.test
{
    /// 
    /// 视图的grid列设置
    /// 
    public class GridSettingCommon
    {
        private string moduleName;
        private string viewName;

        /// 
        /// grid所在模块名称,用以区分同一grid在不同模块中的显示情况
        /// 
        public string ModuleName
        {
            get { return this.moduleName; }
            set { this.moduleName = value; }
        }

        /// 
        /// grid所在视图名称
        /// 
        public string ViewName
        {
            get { return this.viewName; }
            set { this.viewName = value; }
        }

        public GridSettingCommon(string moduleName, string viewName)
        {
            this.moduleName = moduleName;
            this.viewName = viewName;
        }

        /// 
        /// 依据用户ID,获取对应设置
        /// 
        /// 
        /// 
        public List GetGridSettingByName(string xmpPath, string userID = "0", bool isTest = true)
        {
            DataTable dt = this.GetGridSettingDataTable(xmpPath);
            List columnInfoList = new List();

            if (isTest)
            {
                if (dt != null && dt.Columns.Count > 0)
                {
                    int i = 0;
                    foreach (DataColumn col in dt.Columns)
                    {
                        GridColumInfoCommon gridColumn = new GridColumInfoCommon();
                        gridColumn.FieldName = col.ColumnName;
                        gridColumn.Header = col.Caption;
                        gridColumn.VisibleIndex = i++;
                        columnInfoList.Add(gridColumn);
                    }
                }
            }

            return columnInfoList;
        }

        private DataTable GetGridSettingDataTable(string xmpPath, bool isTest = true)
        {
            DataTable dt = new DataTable();
            try
            {
                if (isTest) {
                    string path = this.GetSchemaName(xmpPath);
                    dt.ReadXmlSchema(path);
                }
                   
            }
            catch (Exception ex)
            {
                //System.Windows.MessageBox.Show("获取配置信息失败!错误原因:" + ex.Message);
            }
            return dt;
        }

        private string GetSchemaName(string xmpPath,bool isTest = true)
        {
            string schemaName = string.Empty;
            if (isTest)
            {
                //if (viewName == "StockQuotationByPosition")
                schemaName = AppDomain.CurrentDomain.BaseDirectory + xmpPath;
            }

            return schemaName;
        }
    }
}

       定义列信息:
namespace com.test
{
    public class GridColumInfoCommon
    {
        public string Header { get; set; }
        public string FieldName { get; set; }
        public string BindingPath { get; set; }
        public int VisibleIndex
        {
            get;
            set;
        }
    }

    public class ColumnInfoBehaviorCommon : Behavior
    {
        public static readonly DependencyProperty ColumnInfoProperty =
            DependencyProperty.RegisterAttached("ColumnInfoCommon", typeof(GridColumInfoCommon), typeof(ColumnInfoBehaviorCommon), new PropertyMetadata(null, OnColumnInfoChanged));

        public static GridColumInfoCommon GetColumnInfoCommon(DependencyObject obj)
        {
            return (GridColumInfoCommon)obj.GetValue(ColumnInfoProperty);
        }

        public static void SetColumnInfoCommon(DependencyObject obj, GridColumInfoCommon value)
        {
            obj.SetValue(ColumnInfoProperty, value);
        }

        static void OnColumnInfoChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            GridColumn c = o as GridColumn;
            if (e.NewValue != null)
                Interaction.GetBehaviors(c).Add(new ColumnInfoBehaviorCommon() { Info = (GridColumInfoCommon)e.NewValue });
        }

        public static readonly DependencyProperty InfoProperty =
            DependencyProperty.Register("Info", typeof(GridColumInfoCommon), typeof(ColumnInfoBehaviorCommon),
            new PropertyMetadata(null, (d, e) => ((ColumnInfoBehaviorCommon)d).OnInfoChanged()));

        public GridColumInfoCommon Info
        {
            get { return (GridColumInfoCommon)GetValue(InfoProperty); }
            set { SetValue(InfoProperty, value); }
        }

        void OnInfoChanged()
        {
            if (Info == null) return;
            if (AssociatedObject == null) return;
            if (Info.FieldName != null)
                AssociatedObject.FieldName = Info.FieldName;
            if (Info.BindingPath != null)
                AssociatedObject.Binding = new Binding() { Path = new PropertyPath(Info.BindingPath) };
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            OnInfoChanged();
        }
    }

    public class ColumnTemplateSelectorCommon : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            GridColumnInfo column = (GridColumnInfo)item;
            return (DataTemplate)((Control)container).FindResource("DefaultColumnTemplate");
        }
    }
}

       修改viewmodel,添加列排序定义字段并在构造函数中读取xml:
namespace com.test{
          public class Bviewmodel: INotifyPropertyChanged
          {
              //应该在构造函数为该集合赋一些测试数据
              private ObservableCollection b_vm= new ObservableCollection();
private ObservableCollection bColumns = new ObservableCollection();
              public Bviewmodel()
              {
            GridSettingCommon gridSettingCommon = new GridSettingCommon("test", "test");
            List columnListB = gridSettingCommon.GetGridSettingByName("order.xml");
            if (columnListB != null)
            {
                foreach (GridColumInfoCommon columnInfoCommon in columnListB )
                    this.bColumns.Add(columnInfoCommon);
                columnListB.Clear();
            }
               }
              public ObservableCollection B_vm
              {
                 get { return this.b_vm; }
                 set { this.b_vm= value; }
              }
              public ObservableCollection BColumns
              {
                 get { return this.bColumns ; }
              }
              public event PropertyChangedEventHandler PropertyChanged;

              protected void OnPropertyChanged(string propertyname)
             {
                PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
                if (propertyChanged != null)
                {
                propertyChanged(this, new PropertyChangedEventArgs(propertyname));
                }
             }
        }

              在xaml中绑定列排序定义:

    
        
    
    
    
      

                            
                        
            
                
            
        
    

你可能感兴趣的:(WinForm)