DevExpress,LayoutControl,TreeList,GridControl等


1.顯示邊框進行折疊

選擇一個layoutControlGroupX 將其GroupBordersVisible設置成True,將TextVisiable=True

2. TreeList

2.1需要綁定的數據有ParentFieldName,KeyFieldName,

2.2.1.TeeList前面的連線
啟動程序中添加DevExpress.BonusSkins.v13.1.dll
Main中進行注冊

     [STAThread]
        static void Main(params String[] args)
        {
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.Skins.SkinManager.EnableFormSkins();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (args.Length>=1 && args[0]=="test")
            {
                Application.Run(new Form1());
            }else
            {
                Application.Run(new F.Studio.DevExpressUI.frmLogin());
            }
        }
View Code

 需要設置LookAndFeel(比方Blue),

將UseDefaultLookAndFeel設置成False
//顯示checkBox,並執行遞歸選擇

treeList1.OptionsBehavior.AllowRecursiveNodeChecking = true;
treeList1.OptionsView.ShowCheckBoxes = true;
//參考:http://blog.sina.com.cn/s/blog_53b58e7c0101aclk.html

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using F.Studio.Prime.IService;
using F.Studio.Prime.EFModel;
using DevExpress.XtraTreeList.Nodes;

namespace F.Studio.DevExpressUI
{
    public partial class frmFuncsConfigEdit : frmEditBase
    {
        public Sys_FuncsConfig ModifyConfig { get; set; }

        public frmFuncsConfigEdit()
        {
            InitializeComponent();
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
            
            #region 配置TreeList


            imageList1.Images.Add( Properties.Resources.folder);
            imageList1.Images.Add( Properties.Resources.folder_open);
            imageList1.Images.Add( Properties.Resources.func);
            imageList1.Images.Add( Properties.Resources.tag_16x16);
            imageList1.Images.Add( Properties.Resources.question_16x16);

          
            treeList1.StateImageList = imageList1;
            treeList1.SelectImageList = imageList1;
            treeList1.ColumnsImageList = imageList1;
          
            //顯示checkBox,並執行遞歸選擇
            
            treeList1.OptionsView.ShowCheckBoxes = true;
            treeList1.OptionsBehavior.AllowIndeterminateCheckState = false;
            treeList1.OptionsBehavior.AllowRecursiveNodeChecking = false;
            treeList1.OptionsBehavior.Editable = false;
            
            treeList1.AfterCheckNode += (s, e) =>
            {
                Console.WriteLine(e.Node.SelectImageIndex);
            };
            treeList1.NodesReloaded += (s, e) =>
            {
                //到這里完成全部Node的掛接!
                Console.WriteLine("NodesReloaded");
                
                SetTreeListCheckState();
                treeList1.OptionsBehavior.AllowRecursiveNodeChecking = true;
            };
            treeList1.CreateCustomNode += (s, e) => {
                var tag = e.Tag;
                Console.WriteLine("CreateCustomNode");
                
            };
            treeList1.NodeChanged += (s, e) => {
                Console.WriteLine("NodeChanged");
              
            };
            treeList1.FocusedNodeChanged += (s, e) => {

                var list= treeList1.GetDataRecordByNode(e.Node);
               
                childBindingSource.DataSource = list;
            };
            #endregion


            
        }
        public frmFuncsConfigEdit(Sys_FuncsConfig config):this()
        {
            sys_FuncsConfigBindingSource.DataSource = config;
            IsModify = true;
            ModifyConfig = config;

   
        }
        private void SetTreeListCheckState()
        {
            Queue<TreeListNode> queue = new Queue<TreeListNode>();
            foreach (TreeListNode node in treeList1.Nodes)
            {
                queue.Enqueue(node);
            };

            while (queue.Count > 0)
            {
                var node= queue.Dequeue();
                
                var isChecked=(bool)node.GetValue("IsChecked");
                node.Checked = isChecked;
                 //treeList1.SetNodeCheckState(node, isChecked ? CheckState.Checked : CheckState.Unchecked, false);
                foreach (TreeListNode child in node.Nodes)
                {
                    queue.Enqueue(child);
                }
            }
        
        }
        private void frmFuncsConfigEdit_Load(object sender, EventArgs e)
        {


            Func<List<SysResourceInfo>> fun = () =>
            {
                return SysResourceInfo.Build();
            };
            var list = InvokeService<List<SysResourceInfo>>(fun);
           
            
            if (!IsModify)
            {
                sys_FuncsConfigBindingSource.AddNew();
            }
            else//修改狀態
            {
                
                string wehre=string.Format("it.ConfigId={0}",ModifyConfig.ConfigId);
                var configItems= Fetch<IFuncsConfigItemsService>().GetList(wehre, "it.RecId");
                list.ForEach(item => {
                    if (item.ResourceType == "目錄") return;
                    if (configItems.Any(ent => ent.ResourceId == item.FuncId))
                    {
                        item.IsChecked = true;
                    }
                });

            }
            sysResourceInfoBindingSource.DataSource = list;
            Console.WriteLine(treeList1.AllNodesCount);
       
           
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           
            foreach (TreeListNode node in treeList1.Nodes)
            {
                Console.WriteLine(node.ToString());
            }
        }

        private void btnCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }
}
View Code

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using F.Studio.Prime.IService;
using F.Studio.Prime.EFModel;
using DevExpress.XtraTreeList.Nodes;
using F.Studio.Infrastructure;

namespace F.Studio.DevExpressUI
{
    public partial class frmFuncsConfigEdit : frmEditBase
    {
        public Sys_FuncsConfig ModifyConfig { get; set; }

        public frmFuncsConfigEdit()
        {
            InitializeComponent();
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
            
            #region 配置TreeList


            imageList1.Images.Add( Properties.Resources.folder);
            imageList1.Images.Add( Properties.Resources.folder_open);
            imageList1.Images.Add( Properties.Resources.func);
            imageList1.Images.Add( Properties.Resources.tag_16x16);
            imageList1.Images.Add( Properties.Resources.question_16x16);



            
            treeList1.StateImageList = imageList1;
            treeList1.SelectImageList = imageList1;
            treeList1.ColumnsImageList = imageList1;
          
            //顯示checkBox,並執行遞歸選擇
            
            treeList1.OptionsView.ShowCheckBoxes = true;
            treeList1.OptionsBehavior.AllowIndeterminateCheckState = false;
            treeList1.OptionsBehavior.AllowRecursiveNodeChecking = true;
            treeList1.OptionsBehavior.Editable = false;
            
          
            treeList1.AfterCheckNode += (s, e) =>
            {
                treeList1.FocusedNode = e.Node;
                Console.WriteLine("AfterCheckNode!");
                var r = treeList1.GetDataRecordByNode(e.Node) as SysResourceInfo;
 
                r.IsChecked = e.Node.Checked;
                treeList1.CloseEditor();

               #region (廢棄) 如果窗體取消選擇,那么取消窗體下的按鈕
                //if (!e.Node.Checked)
                //{
                //    if (r.ResourceType == "窗體")
                //    {
                //        r.Buttons.ForEach(ent => ent.IsChecked = false);
                //        buttonsBindingSource.ResetBindings(true);
                //    }
                //}
               #endregion


            };
            treeList1.NodesReloaded += (s, e) =>
            {
                //到這里完成全部Node的掛接!
                Console.WriteLine("NodesReloaded");

                SetTreeListCheckState();
               
            };
            treeList1.CreateCustomNode += (s, e) => {
                //var tag = e.Tag;
                //Console.WriteLine("CreateCustomNode");
                
            };
            treeList1.NodeChanged += (s, e) => {
                //Console.WriteLine("NodeChanged");
              
            };
            treeList1.FocusedNodeChanged += (s, e) => {

                var list= treeList1.GetDataRecordByNode(e.Node);
               
               buttonsBindingSource.DataSource = list;
            };

            buttonsBindingSource.CurrentItemChanged += (s, e) => {
                //var btn= buttonsBindingSource.Current as SysButtonInfo;
                //if (btn == null) return;
                //Console.WriteLine("CurrentItemChanged:" + btn.IsChecked);
            };
            #endregion


            repositoryItemCheckEdit1.CheckedChanged += (s, e) => {
                Console.WriteLine("CheckedChanged:");
                var btn= gridView1.GetFocusedRow() as SysButtonInfo;
                var parent= btn.Parent as SysResourceInfo;
              
                if((s as CheckEdit).Checked)
                {
                    parent.Node.Checked = true;
                        
                }

               gridView1.CloseEditor();  
               gridView1.UpdateCurrentRow(); 
            };
        }
        public frmFuncsConfigEdit(Sys_FuncsConfig config):this()
        {
            sys_FuncsConfigBindingSource.DataSource = config;
            IsModify = true;
            ModifyConfig = config;

   
        }
        private void SetTreeListCheckState()
        {
            Queue<TreeListNode> queue = new Queue<TreeListNode>();
            foreach (TreeListNode node in treeList1.Nodes)
            {
                queue.Enqueue(node);
            };

            while (queue.Count > 0)
            {
                var node= queue.Dequeue();
                var obj= treeList1.GetDataRecordByNode(node) as SysResourceInfo; 
                var isChecked=(bool)node.GetValue("IsChecked");
                obj.Node = node;//建立引用
                node.Checked = isChecked;
                if (isChecked)
                {
                    var cur = node;
                    while (cur.ParentNode != null)
                    {
                        var rnode = treeList1.GetDataRecordByNode(cur) as SysResourceInfo;
                        Console.WriteLine(rnode.Title);
                        cur.ParentNode.Checked = true;
                        cur = cur.ParentNode;
                    }
                }
                 treeList1.SetNodeCheckState(node, isChecked ? CheckState.Checked : CheckState.Unchecked, false);
                foreach (TreeListNode child in node.Nodes)
                {
                    queue.Enqueue(child);
                }
            }
        
        }
        /// <summary>
        /// 同步接點狀態到綁定數據源
        /// </summary>
        private void SyncCheckState()
        {
            Queue<TreeListNode> queue = new Queue<TreeListNode>();
            foreach (TreeListNode node in treeList1.Nodes)
            {
                queue.Enqueue(node);
            };

            while (queue.Count > 0)
            {
                var node = queue.Dequeue();
                var obj = treeList1.GetDataRecordByNode(node) as SysResourceInfo;
                obj.IsChecked = node.Checked;
                foreach (TreeListNode child in node.Nodes)
                {
                    queue.Enqueue(child);
                }
            }
        }

        private void frmFuncsConfigEdit_Load(object sender, EventArgs e)
        {


            Func<List<SysResourceInfo>> fun = () =>
            {
                return SysResourceInfo.Build();
            };
            var list = InvokeService<List<SysResourceInfo>>(fun);

           
            
            if (!IsModify)
            {
                sys_FuncsConfigBindingSource.AddNew();
            }
            else//修改狀態
            {
                
                string wehre=string.Format("it.ConfigId={0}",ModifyConfig.ConfigId);
                var configItems= Fetch<IFuncsConfigItemsService>().GetList(wehre, "it.RecId");
                list.ForEach(item => {
                    if (item.ResourceType == "目錄") return;
                    if (configItems.Any(ent => ent.ResourceId == item.FuncId))
                    {
                        item.IsChecked = true;

                    }
                    //設置按鈕
                    item.Buttons.ForEach(btn =>
                    {
                        if (configItems.Any(ent1 => ent1.ResourceId == btn.FuncId))
                        {
                            btn.IsChecked = true;
                        }
                    });
                });

            }
            sysResourceInfoBindingSource.DataSource = list;


            treeList1.ExpandAll();
            //SetTreeListCheckState();
       
           
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                sys_FuncsConfigBindingSource.EndEdit();
                sysResourceInfoBindingSource.EndEdit();
                buttonsBindingSource.EndEdit();
                gridView1.CloseEditor();

                var ent = sys_FuncsConfigBindingSource.Current as Sys_FuncsConfig;
                if (string.IsNullOrWhiteSpace(ent.ConfigName)) throw new Exception("請提供配置名!");
 
            
                var srv = ServiceLocator.Fetch<IFuncsConfigService>();
                if (IsModify)
                {
                    srv.Update(ent);
                }
                else
                {
                    var newEnt = srv.Add(ent);
                    ent.ConfigId = newEnt.ConfigId;

                    base.NewRecord = newEnt;
                }

                #region 更新配置



                SyncCheckState();
                var res = (sysResourceInfoBindingSource.DataSource as List<SysResourceInfo>).Where(entTmp=>entTmp.ResourceType!="目錄").ToList();

                
                #region(作廢) 被"目錄"類型節點遞歸選中的節點做額外處理
                //var nodes = treeList1.GetAllCheckedNodes();
                //nodes.ForEach(node =>
                //{
                //    var resIt = treeList1.GetDataRecordByNode(node) as SysResourceInfo;
                //    if (resIt.ResourceType != "目錄")
                //    {
                //        resIt.IsChecked = true;
                       
                //    }
                //});
                #endregion

                Action act = () => {

                    var newResList = new List<Sys_FuncsConfigItems>();

                    foreach (var r in res)
                    {
                       
                        var newRes = new Sys_FuncsConfigItems();
                        newRes.ResourceId = r.FuncId;
                        newRes.ResourceType = r.ResourceType;
                        newRes.ConfigId = ent.ConfigId;
                        if (r.IsChecked.Value)
                        {
                            newResList.Add(newRes);
                        }

                        foreach (var btn in r.Buttons)
                        {
                            if (btn.IsChecked.Value)
                            {
                                var newBtnRes = new Sys_FuncsConfigItems();
                                newBtnRes.ResourceId = btn.FuncId;
                                newBtnRes.ResourceType = btn.ResourceType;
                                newBtnRes.ConfigId = ent.ConfigId;
                                newResList.Add(newBtnRes);
                            }
                        }
                    }
                    ServiceLocator.Fetch<IFuncsConfigItemsService>().SetConfigItems(newResList, ent);
                };
                InvokeService(act);

                #endregion
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch (Exception ex)
            {
                ErrMsg(ex.Message);

            }

        }

        private void btnCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
    }
}
View Code

 展開關閉圖標

           imgList = new ImageList(this.components);
            imgList.Images.Add(Properties.Resources.folder);
            imgList.Images.Add(Properties.Resources.folder_open);
            treeList1.SelectImageList = imgList;
            treeList1.StateImageList = imgList;
            
            treeList1.AfterCollapse += (s, e) => {
                e.Node.ImageIndex = 0;
                e.Node.SelectImageIndex = 0;
            };
            treeList1.AfterExpand += (s, e) => {
                e.Node.ImageIndex = 1;
                e.Node.SelectImageIndex = 1;
            };
View Code

 

 3.GridControl

 3.1.1添建按鈕列->參考:http://www.cnblogs.com/zeroone/p/3534783.html

3.1.2 下拉框編輯->http://www.cnblogs.com/zeroone/p/3606224.html

 3.2 設置參考列:http://blog.csdn.net/nanchuan/article/details/7664070

            gridView1.CustomColumnDisplayText += (s, e) =>
            {
                if (e.Column.FieldName == "FuncConfigId")
                {
                    var list = FuncsConfigLookUp.Value;
                    if (list != null)
                    {
                        var it = list.FirstOrDefault(ent => ent.ConfigId == int.Parse(e.Value.ToString()));
                        if (it != null) e.DisplayText = it.ConfigName;
                    }
                }
            };
View Code

3.3 設置計算列,分組會總等
參考:http://blog.csdn.net/lenovouser/article/details/7405577

gridColumn1.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
gridColumn1.UnboundExpression = "[Amount] * ToDouble(SubString([CostPerUnit],0, CharIndex('(',[CostPerUnit])))";
gridView1.GroupSummary.Add(SummaryItemType.Sum, "gridColumn1", gridView1.Columns["gridColumn1"],"組計:{0}");
注:gridColumn1是一個未幫定列,另外需要開啟gridView.OptionsView.ShowFooter=True

 

3.4判斷gridControl 的那個列的單元格被雙擊
 

    gridControl1.MouseDoubleClick += (s, e) =>
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    GridHitInfo hInfo = gridView1.CalcHitInfo(new Point(e.X, e.Y));
                    if (!hInfo.InRow) return; //確保點的是數據行而不是標頭
                   
                    var ent = qSymptomRepositoryItemBindingSource.Current as Q_SymptomRepositoryItem;
                    if (ent == null) return;

                    //hInfo.Column==null 標示點了行頭
                    if (hInfo.Column!=null && hInfo.Column.Caption == "圖片")
                    {
                        var img=gridView1.GetFocusedValue() as Image;
                        new frmPhotoDetail(img).ShowDialog();
                        return;
                    }
                   
                    //雙擊其他標示修改
                    btnModify.PerformClick();
                    
                }
            };
View Code

3.5 啟用滾動條 gridView1.OptionsView.ColumnAutoWidth = false;

 

4.窗體繼承

 

4.1.如果顯示設計時錯誤,需要把兩個partial 類的父類都設置下
4.2.如果father是泛型類那么會一直顯示設計時錯誤

4.3.父親窗體中用From_Load 或重寫了OnLoad時,里面不要寫代碼,如果要寫需要加入if(!DesignMode){...}
4.4.在繼承窗體上增加按鈕,無法顯示

5.樣式與中文包


將zn-CN 復制到運行目錄 執行下列代碼

        /// </summary>
        [STAThread]
        static void Main(params String[] args)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");
            DevExpress.UserSkins.BonusSkins.Register();
            DevExpress.Skins.SkinManager.EnableFormSkins();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (args.Length >= 1 && args[0] == "test")
            {
                Application.Run(new Form1());
            }
            else
            {
                Application.Run(new F.Studio.DevExpressUI.frmLogin());
            }
        }
View Code

 

 6.DateEdit顯示時間

DateEdit如果開啟Vista模式並顯示日期+時間模式 問題,以前沒有涉及過,借機看一下,記錄如下:
設置為Vista顯示模式(如下圖)

 

 參考 :http://www.cnblogs.com/xiaofengfeng/archive/2011/09/07/2169691.html

設置以下屬性
dateEdit1.Properties.VistaDisplayMode = DevExpress.Utils.DefaultBoolean.True;
dateEdit1.Properties.VistaEditTime = DevExpress.Utils.DefaultBoolean.True;

設置顯示長日期模式(日期+時間):
dateEdit1.Properties.DisplayFormat.FormatString="g"
dateEdit1.Properties.DisplayFormat.FormatType=DateTime
dateEdit1.Properties.EditFormat.FormatString="g"
dateEdit1.Properties.EditFormat.FormatType=DateTime

------------------------------------------------------------------

dateEdit1.Properties.VistaDisplayMode = DevExpress.Utils.DefaultBoolean.True;
dateEdit1.Properties.VistaEditTime = DevExpress.Utils.DefaultBoolean.True;
dateEdit1.Properties.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
dateEdit1.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
dateEdit1.Properties.EditFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
dateEdit1.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
dateEdit1.Properties.EditMask = "yyyy-MM-dd HH:mm:ss";



設置為Vista模式時,如果要顯示日期+時間的長日期模式,還需要設置:

VistaTimeProperties.DisplayFormat
VistaTimeProperties.EditFormat

 

7.treeListLookUpEdit禁止文本框輸入

  catalogIdSpinEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM