开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 14704|回复: 4

DEV 12.2.7 实现 TreeListLookUp控件

[复制链接]

0

精华

105

贡献

62

赞扬

帖子
104
软币
876
在线时间
101 小时
注册时间
2013-8-21
发表于 2014-9-9 23:33:20 | 显示全部楼层 |阅读模式
DEV 比较低的版本不包含TreeListLookUp 控件,想要使用只能自定义该控件了
下面贴出具体的过程
RepositoryItemTreeListLookUpEdit 类
[C#] 纯文本查看 复制代码
 [UserRepositoryItem("Register")]
    public class RepositoryItemTreeListLookUpEdit : RepositoryItemPopupBaseAutoSearchEdit
    {
        #region Register, Assign, Dispose, CheckDestroyDataSource
        internal const string EditorName = "TreeListLookUpEdit";

        public static void Register()
        {
            EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(EditorName, typeof(TreeListLookUpEdit),
                typeof(RepositoryItemTreeListLookUpEdit), typeof(PopupBaseAutoSearchEditViewInfo),
                new ButtonEditPainter(), true, null));
        }
        static RepositoryItemTreeListLookUpEdit()
        {
            Register();
        }
        public override string EditorTypeName
        {
            get { return EditorName; }
        }

        public override void Assign(RepositoryItem item)
        {
            RepositoryItemTreeListLookUpEdit source = item as RepositoryItemTreeListLookUpEdit;
            BeginUpdate();
            try
            {
                base.Assign(item);
                this.DisplayMember = source.DisplayMember;
                this.ValueMember = source.ValueMember;
                this.DataSource = source.DataSource;
                this.ImmediatePopupForm = source.ImmediatePopupForm;
                this.EnableFilter = source.EnableFilter;
                this.CaseSensitiveSearch = source.CaseSensitiveSearch;
                this.SearchMode = source.SearchMode;
            }
            finally
            {
                EndUpdate();
            }
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                CheckDestroyDataSource();
            }
            base.Dispose(disposing);
        }
        private void CheckDestroyDataSource()
        {
            try
            {
                if (dataSource != null)
                {
                    IDisposable ds = dataSource as IDisposable;
                    if (ds != null) ds.Dispose();
                }

                this.DataSource = null;
            }
            catch { }
        }
        #endregion
        // ==============================================================
        const string DefaultNullText = "[EditValue is null]";
        TreeListInner treeList;
        // Fields...
        private string displayMember;
        private string valueMember;
        private object dataSource;
        private SearchMode searchMode;
        bool caseSensitiveSearch;
        private bool enableFilter;
        private bool immediatePopupForm;
        static readonly object processNewValue = new object();

        internal bool EnableFilter
        {
            get { return enableFilter; }
            set { enableFilter = value; }
        }

        internal TreeListNode PrevFocusedNode { get; set; }
        
        internal IDictionary PropertiesStore
        {
            get { return base.PropertyStore; }
        }

        public TreeListInner TreeList { get { return treeList; } }

        #region Designer predefined Properties
        [DXCategory(CategoryName.Data), DefaultValue(null),
#if DXWhidbey
		AttributeProvider(typeof(IListSource))]
#else
 TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design")]
#endif
        public object DataSource
        {
            get { return dataSource; }
            set
            {
                if (DataSource != value)
                {
                    dataSource = value;
                    if (TreeList.BindingContext == null)
                        TreeList.BindingContext = new BindingContext();
                    if (TreeList.IsHandleCreated)
                        TreeList.DataSource = value;
                    OnPropertiesChanged();
                }
            }
        }
        
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public bool ImmediatePopupForm
        {
            get { return immediatePopupForm; }
            set { immediatePopupForm = value; }
        }
        
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override bool ImmediatePopup { get { return ImmediatePopupForm; } set { ImmediatePopupForm = value; } }

        [DefaultValue(false)]
        public bool CaseSensitiveSearch
        {
            get { return caseSensitiveSearch; }
            set
            {
                if (CaseSensitiveSearch == value) return;
                caseSensitiveSearch = value;
                OnPropertiesChanged();
            }
        }

        [DXCategory(CategoryName.Data),
        DefaultValue(""),
#if DXWhidbey
		TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design"),
		Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))]
#else
 TypeConverter("DevExpress.XtraEditors.Design.DataMemberTypeConverter, " + AssemblyInfo.SRAssemblyEditorsDesign)]
#endif
        public string DisplayMember
        {
            get { return displayMember; }
            set
            {
                if (displayMember != value)
                {
                    displayMember = value;
                    OnPropertiesChanged();
                }
            }
        }

        [DXCategory(CategoryName.Data),
        DefaultValue(""),
#if DXWhidbey
		TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design"),
		Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))]
#else
 TypeConverter("DevExpress.XtraEditors.Design.DataMemberTypeConverter, " + AssemblyInfo.SRAssemblyEditorsDesign)]
#endif
        public virtual string ValueMember
        {
            get { return valueMember; }
            set
            {
                if (valueMember != value)
                {
                    valueMember = value;
                    OnPropertiesChanged();
                }
            }
        }

        [DXCategory(CategoryName.Behavior), DefaultValue(SearchMode.OnlyInPopup)]
        public SearchMode SearchMode
        {
            get { return searchMode; }
            set
            {
                if (SearchMode == value) return;
                searchMode = value;
                UpdateSearchMode();
            }
        }
        #endregion

        public new TreeListLookUpEdit OwnerEdit { get { return base.OwnerEdit as TreeListLookUpEdit; } }

        public RepositoryItemTreeListLookUpEdit()
        {
            if (NullText == null || NullText == "")
                NullText = DefaultNullText;
            this.valueMember = this.displayMember = string.Empty;
            treeList = CreateTreeList();
            treeList.OptionsBehavior.Editable = false;
            TreeList.OptionsBehavior.ExpandNodesOnIncrementalSearch = true;
            TreeList.OptionsFilter.FilterMode = FilterMode.Smart;
            ImmediatePopup = true;
            TreeList.OptionsBehavior.EnableFiltering = true;
            filteringNodes = new List<TreeListNode>();
            UpdateSearchMode();
        }

        protected virtual void UpdateSearchMode()
        {
            EnableFilter = false;
            TreeList.OptionsBehavior.AllowIncrementalSearch = false;
            ImmediatePopupForm = false; // ?
            switch (searchMode)
            {
                case SearchMode.AutoFilter:
                    EnableFilter = true;
                    ImmediatePopupForm = true;
                    break;
                case SearchMode.AutoComplete:
                    SetCriteriaOperator(null);
                    break;
                case SearchMode.OnlyInPopup:
                    TreeList.OptionsBehavior.AllowIncrementalSearch = true;
                    SetCriteriaOperator(null);
                    break;
            }
        }

        internal void SetCriteriaOperator(string text)
        {
            CriteriaOperator cp = CriteriaOperator.Parse(text);
            this.TreeList.ActiveFilterCriteria = cp;
        }

        private TreeListInner CreateTreeList() { return new TreeListInner(); }

        internal void RefreshDataSource()
        {
            TreeList.DataSource = DataSource;
        }

        bool IsMemberValid(string member)
        {
            if (TreeList.Columns[member] != null)
                return true;
            return false;
        }

        public TreeListNode GetNodeByEditValue(string text)
        {
            return GetNodeByDisplayText(TreeList.Nodes, text, ValueMember);
        }

        internal TreeListNode GetNodeByText(string value)
        {
            return GetNodeByDisplayText(TreeList.Nodes, value, DisplayMember);
        }

        internal TreeListNode GetNodeByDisplayText(TreeListNodes nodes, string text, string member)
        {
            TreeListNode nod = null;
            if (string.IsNullOrEmpty(member)) return null;
            if (IsMemberValid(member) == false) return null;
            if (text == "" || text == null) return null;
            for (int i = 0; i < nodes.Count; i++)
            {
                string s = nodes[i].GetDisplayText(member);
                s = s.Substring(0, Math.Min(text.Length, s.Length));
                if (!CaseSensitiveSearch)
                {
                    text = text.ToUpper();
                    s = s.ToUpper();
                }
                if (s == text)
                {
                    nod = nodes[i];
                    break;
                }
                if (nodes.Count != 0)
                    nod = GetNodeByDisplayText(nodes[i].Nodes, text, member);
                if (nod != null) break;
            }
            return nod;
        }

        internal void GetNodesByDisplayText(TreeListNodes nodes, string text)
        {
            for (int i = 0; i < nodes.Count; i++)
            {
                string s = nodes[i].GetDisplayText(DisplayMember); // -!
                s = s.Substring(0, Math.Min(text.Length, s.Length));
                if (!CaseSensitiveSearch)
                {
                    text = text.ToUpper();
                    s = s.ToUpper();
                }
                if (s == text)
                    filteringNodes.Add(nodes[i]);
                if (nodes.Count != 0)
                    GetNodesByDisplayText(nodes[i].Nodes, text);
            }
        }

        void ShowParents(TreeListNode node)
        {
            if (node.ParentNode != null)
            {
                ShowParents(node.ParentNode);
                node.ParentNode.Visible = true;
            }
        }

        internal void ShowNodePath()
        {
            for (int i = 0; i < filteringNodes.Count; i++)
            {
                ShowParents(filteringNodes[i]);
            }
        }

        List<TreeListNode> filteringNodes;
        internal void ExpandNodesForFiltering(string text)
        {
            filteringNodes.Clear();
            if (text == "")
            {
                treeList.CollapseAll();
                return;
            }
            GetNodesByDisplayText(TreeList.Nodes, text);
            if (filteringNodes.Count == 0) return;
            for (int i = 0; i < filteringNodes.Count; i++)
            {
                ExpandNode(filteringNodes[i], filteringNodes[i].Level);
            }
        }

        void ExpandNode(TreeListNode node, int level)
        {
            if (level == 0) return;
            if (node.ParentNode != null)
            {
                if (node.ParentNode.Expanded == false)
                {
                    ExpandNode(node.ParentNode, level);
                    node.ParentNode.Expanded = true;
                }
            }
            else
                node.Expanded = true;
        }

        public virtual object GetFocusedValue()
        {
            if (TreeList.Columns[ValueMember] != null && TreeList.FocusedNode != null)
                return TreeList.FocusedNode.GetValue(ValueMember);
            return NullText;
        }

        private TreeListNode FindValueNode(object editValue)
        {
            TreeListNode node = null;
            node = TreeList.FindNodeByFieldValue(ValueMember, editValue);
            return node;
        }

        private string GetTreeLookUpDisplayText(FormatInfo format, object editValue)
        {
            if (IsNullValue(editValue)) return GetNullText(format);
            TreeListNode node = FindValueNode(editValue);
            if (node != null)
                return node.GetDisplayText(DisplayMember);
            return "";
        }

        public override string GetDisplayText(FormatInfo format, object editValue)
        {
            string displayText = GetTreeLookUpDisplayText(format, editValue);
            if (displayText == "")
            {
                if (OwnerEdit == null)
                {
                    return base.GetDisplayText(format, editValue);
                }
                return OwnerEdit.AutoSearchText;
            }
            CustomDisplayTextEventArgs e = new CustomDisplayTextEventArgs(editValue, displayText);
            if (format != EditFormat)
                RaiseCustomDisplayText(e);
            return e.DisplayText;
        }

        [DXCategory(CategoryName.Events)]
        public event ProcessNewValueEventHandler ProcessNewValue
        {
            add { this.Events.AddHandler(processNewValue, value); }
            remove { this.Events.RemoveHandler(processNewValue, value); }
        }

        protected internal virtual void RaiseProcessNewValue(ProcessNewValueEventArgs e)
        {
            ProcessNewValueEventHandler handler = (ProcessNewValueEventHandler)Events[processNewValue];
            if (handler != null) handler(GetEventSender(), e);
        }
    }

TreeListInner类
[C#] 纯文本查看 复制代码
        protected override TreeListOptionsBehavior CreateOptionsBehavior()
        {
            return new TreeLookUpOptionsBehavior();
        }

        [Browsable(false)]
        new public object DataSource
        {
            get { return base.DataSource; }
            set
            {
                if (DataSource == value) return;
                base.DataSource = value;
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            TreeListHitInfo hi = this.CalcHitInfo(e.Location);
            TreeListLookUpPopupForm ownerForm = (Parent as TreeListLookUpPopupForm);
            if (hi.Node != null && hi.Column != null)
                ownerForm.Properties.OwnerEdit.HidePopup();
            else
                ownerForm.Properties.OwnerEdit.EditValue = ownerForm.Properties.GetFocusedValue();
        }


TreeListLookUpEdit类
[C#] 纯文本查看 复制代码
public class TreeListLookUpEdit : PopupBaseAutoSearchEdit
    {
        static TreeListLookUpEdit()
        {
            RepositoryItemTreeListLookUpEdit.Register();
        }

        public TreeListLookUpEdit() {}

        public override string EditorTypeName
        {
            get { return RepositoryItemTreeListLookUpEdit.EditorName; }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemTreeListLookUpEdit Properties
        {
            get { return base.Properties as RepositoryItemTreeListLookUpEdit; }
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            Properties.TreeList.CreateControl();
            Properties.RefreshDataSource();
        }

        protected override void OnPopupShown()
        {
            base.OnPopupShown();
            if (Properties.SearchMode == SearchMode.OnlyInPopup)
                Properties.TreeList.Focus();
        }

        string filterText = "";
        internal string GetFilterText() { return filterText; }
        protected override void ProcessFindItem(KeyPressHelper helper, char pressedKey)
        {
            filterText = helper.Text;
            if (filterText == "" || filterText == null)
                Properties.TreeList.FocusedNode = Properties.PrevFocusedNode;
            base.ProcessFindItem(helper, pressedKey);
            if (filterText != "" && Properties.SearchMode != SearchMode.OnlyInPopup)
            {
                TreeListNode node = Properties.GetNodeByDisplayText(Properties.TreeList.Nodes, filterText, Properties.DisplayMember);
                if (node != null)
                {
                    string nodeText = node.GetDisplayText(Properties.DisplayMember);
                    EditValue = node[Properties.ValueMember];
                    AutoSearchText = filterText;
                    this.SelectionStart = helper.GetCorrectedAutoSearchSelectionStart(Text, pressedKey);
                    this.SelectionLength = nodeText.Length - this.SelectionStart;
                    if (Properties.TreeList.FocusedNode != null)
                        if (Properties.TreeList.FocusedNode != Properties.PrevFocusedNode)
                            Properties.PrevFocusedNode = Properties.TreeList.FocusedNode;
                    Properties.TreeList.FocusedNode = node;
                    LayoutChanged();
                }
            }
        }

        protected override void OnEditValueChanged()
        {
            base.OnEditValueChanged();
            Properties.SetCriteriaOperator(null);
            if (Properties.EnableFilter && Properties.TreeList.Columns[Properties.DisplayMember] != null &&
                                           Properties.TreeList.Columns[Properties.ValueMember] != null &&
                                           filterText != "")
            {
                Properties.ExpandNodesForFiltering(filterText);
                if (Convert.ToString(EditValue) == "" || EditValue == null)
                    filterText = "";
                if (filterText != null && filterText != "")
                {
                    string criteriaString = "StartsWith([" + Properties.TreeList.Columns[Properties.DisplayMember].FieldName + "], '" + filterText + "')";
                    Properties.SetCriteriaOperator(criteriaString);
                }
                Properties.ShowNodePath();
            }
        }

        protected override void OnValidating(CancelEventArgs e)
        {
            base.OnValidating(e);
            if (Properties.GetNodeByText(filterText) == null && filterText != "")
            {
                ProcessNewValueEventArgs ee = new ProcessNewValueEventArgs(filterText);
                Properties.RaiseProcessNewValue(ee);
                if (ee.Handled) { EditValue = Properties.GetNodeByText(filterText)[Properties.ValueMember]; }
            }
            HidePopup();
        }

        protected override void DoImmediatePopup(int itemIndex, char pressedKey)
        {
            if (pressedKey != '\b' && Properties.ImmediatePopupForm)
                ShowPopup();
        }

        public void HidePopup()
        {
            ClosePopup();
        }

        protected override void DoClosePopup(PopupCloseMode closeMode)
        {
            base.DoClosePopup(closeMode);
            Properties.TreeList.StopIncrementalSearch();
        }

    [DXCategory(CategoryName.Events)]
		public event ProcessNewValueEventHandler ProcessNewValue {
			add { this.Properties.ProcessNewValue += value; }
			remove { this.Properties.ProcessNewValue -= value; }
		}

        protected override PopupBaseForm CreatePopupForm()
        {
            return new TreeListLookUpPopupForm(this);
        }
    }


TreeListLookUpPopupForm类
[C#] 纯文本查看 复制代码
class TreeListLookUpPopupForm : CustomBlobPopupForm 
    {
        internal static int SmallIndent = 10;

        public new RepositoryItemTreeListLookUpEdit Properties { get { return OwnerEdit.Properties as RepositoryItemTreeListLookUpEdit; } }
        protected TreeList TreeList { get { return Properties.TreeList; } }

        public TreeListLookUpPopupForm(PopupBaseEdit ownerEdit)
            : base(ownerEdit)
        {
            this.TreeList.Bounds = new System.Drawing.Rectangle(SmallIndent/2, SmallIndent/2, 0, 0);
            Controls.Add(TreeList);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Size treeListSize = new Size();
            treeListSize.Width = this.Size.Width - SmallIndent;
            treeListSize.Height = this.Size.Height - this.ViewInfo.SizeBarRect.Height - SmallIndent;
            TreeList.Size = treeListSize;
        }

        public override void ShowPopupForm()
        {
            base.ShowPopupForm();
            if(Properties.SearchMode == SearchMode.OnlyInPopup)
                TreeList.Focus();
        }

        protected override void Dispose(bool disposing)
        {
            TreeList.Parent = null;
            base.Dispose(disposing);
        }

        protected override Size CalcFormSizeCore()
        {
            Size s = base.CalcFormSizeCore();
            object blobSize = (OwnerEdit.Properties as RepositoryItemTreeListLookUpEdit).PropertiesStore["BlobSize"];
            if (blobSize == null)
                s.Width = Math.Max(DefaultEmptySize.Width, OwnerEdit.Width);
            return s;
        }

        protected override object QueryResultValue()
        {
            return Properties.GetFocusedValue();
        }
        
        public override void ProcessKeyDown(KeyEventArgs e)
        {
            base.ProcessKeyDown(e);
            string dispVal = (OwnerEdit as TreeListLookUpEdit).GetFilterText();
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (Properties.GetNodeByEditValue(dispVal) == null && dispVal != "")
                    {
                        ProcessNewValueEventArgs ee = new ProcessNewValueEventArgs(dispVal);
                        Properties.RaiseProcessNewValue(ee);
                        if (ee.Handled) { OwnerEdit.EditValue = Properties.GetNodeByText(dispVal)[Properties.ValueMember]; }
                    }
                    (this.OwnerEdit as TreeListLookUpEdit).HidePopup();
                    break;
                case Keys.Down:
                    if (TreeList.Focused == false)
                    {
                        TreeList.Focus();
                        if (TreeList.Nodes.Count != 0)
                            if (TreeList.FocusedNode.NextNode != null && TreeList.FocusedNode.NextNode.Visible == true)
                                TreeList.FocusedNode = TreeList.FocusedNode.NextNode;
                    }
                    break;
                case Keys.Up:
                    if (TreeList.Focused == false)
                    {
                        TreeList.Focus();
                        if (TreeList.Nodes.Count != 0)
                            if (TreeList.FocusedNode.PrevNode != null && TreeList.FocusedNode.PrevNode.Visible == true)
                                TreeList.FocusedNode = TreeList.FocusedNode.PrevNode;
                    }
                    break;
            }
        }
    }


TreeLookUpOptionsBehavior类
[C#] 纯文本查看 复制代码
public class TreeLookUpOptionsBehavior : TreeListOptionsBehavior
    {
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        new public bool EnableFiltering
        {
            get { return base.EnableFiltering; }
            set
            {
                if (EnableFiltering == value) return;
                base.EnableFiltering = value;
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        new public bool AllowIncrementalSearch
        {
            get { return base.AllowIncrementalSearch; }
            set
            {
                if (AllowIncrementalSearch == value) return;
                base.AllowIncrementalSearch = value;
            }
        }
    }

将上面的类制作成程序集生成动态链接库就可以调用了
附上但最终调用结果

gridcontrol内

gridcontrol内


lookupediter内

lookupediter内


以上程序出自DEV官网实例文件
链接地址http://www.devexpress.com/example=E4319

评分

参与人数 1贡献 +5 赞扬 +1 收起 理由
羽叶 + 5 + 1 很给力

查看全部评分

回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
25
软币
246
在线时间
24 小时
注册时间
2013-12-20
发表于 2014-10-17 20:43:33 | 显示全部楼层
Good Examples....................................................
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
11
软币
67
在线时间
0 小时
注册时间
2016-1-21
发表于 2016-1-21 16:12:39 | 显示全部楼层
好好好好好好好好好好好好好好好好好
回复

使用道具 举报

0

精华

0

贡献

588

赞扬

帖子
95
软币
1260
在线时间
79 小时
注册时间
2017-8-2
发表于 2024-2-21 17:22:29 | 显示全部楼层
Thanks for Sharing.
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-12-22 09:21

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表