.net 关于使用基础设施控制进行列表排序的建议

.net 关于使用基础设施控制进行列表排序的建议,.net,winforms,infragistics,.net,Winforms,Infragistics,我工作的新公司在他们的Winforms应用程序中使用Infrastics控件,我对此没有经验。我需要排序一个列表(最好是通过拖放),以推断列表中项目的优先级。这可以通过intragistics winforms控件完成吗 谢谢 Andy您可以通过一些控件来实现这一点,尽管它们没有内置此功能。下面是一个派生的UltraWinListView,它带有一个DrawFilter,在移动您可以使用的项目时显示高亮显示: 这是DragListView的代码: using Infragistics.Win.U

我工作的新公司在他们的Winforms应用程序中使用Infrastics控件,我对此没有经验。我需要排序一个列表(最好是通过拖放),以推断列表中项目的优先级。这可以通过intragistics winforms控件完成吗

谢谢
Andy

您可以通过一些控件来实现这一点,尽管它们没有内置此功能。下面是一个派生的UltraWinListView,它带有一个DrawFilter,在移动您可以使用的项目时显示高亮显示:

这是DragListView的代码:

using Infragistics.Win.UltraWinListView;
using System;
using System.Drawing;
using System.Windows.Forms;
using Infragistics.Shared;
using System.Reflection;

namespace WindowsFormsApplication1
{
    public class DragListView : UltraListView
    {
        private DragListViewDrawFilter m_oDrawFilter = new DragListViewDrawFilter();

        // stores a boolean that will determine when the mouse is moved if there is something to drag.
        bool allowDrag = false;
        // stores the item to drag
        UltraListViewItem itemToDrag = null;
        // stores the point that the initial mouse down occurred
        Point startPoint;

        internal UltraListViewItem ItemToDrag
        {
            get { return this.itemToDrag; }
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (!this.DesignMode)
            {
                // Wire up events
                this.DragDrop += new System.Windows.Forms.DragEventHandler(DragListView_DragDrop);
                this.DragLeave += new EventHandler(DragListView_DragLeave);
                this.DragOver += new System.Windows.Forms.DragEventHandler(DragListView_DragOver);
                this.MouseDown += new System.Windows.Forms.MouseEventHandler(DragListView_MouseDown);
                this.MouseLeave += new EventHandler(DragListView_MouseLeave);
                this.MouseMove += new System.Windows.Forms.MouseEventHandler(DragListView_MouseMove);
                this.MouseUp += new System.Windows.Forms.MouseEventHandler(DragListView_MouseUp);


                this.m_oDrawFilter.Invalidate += new EventHandler(m_oDrawFilter_Invalidate);

                // Allow dropping on the control
                this.AllowDrop = true;

                m_oDrawFilter.DropLineColor = Color.Blue;
                m_oDrawFilter.DropLineWidth = 3;

                this.DrawFilter = m_oDrawFilter;
            }
        }

        void m_oDrawFilter_Invalidate(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        void DragListView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            // get the item being dropped onto if one exists (The coordinates for this event are screen coordinates so they need to be tranlated to client coordinates)
            UltraListViewItem dropTarget = m_oDrawFilter.DropHighlightItem;

            // only if we have an item that we are dropping on do we do anything
            if (dropTarget != null)
            {
                // if dropLinePosition is AboveItem, then we will drop the item on the targetItems index
                int indexModifier = 0;
                if (this.m_oDrawFilter.dropLinePosition == ListViewDropLinePositionEnum.BelowItem)
                {
                    indexModifier = 1; // the index of the dropped item should be one more than the targetItem
                }
                UltraListViewItem itemToMove = e.Data.GetData(typeof(UltraListViewItem)) as UltraListViewItem;
                if (itemToMove != null)
                {
                    // move the item by removing it from the list and reinserting at the index of the dropTarget added to the indexModifier
                    this.Items.Remove(itemToMove);
                    this.Items.Insert(dropTarget.Index + indexModifier, itemToMove);
                    itemToMove.Group = dropTarget.Group;
                }
            }
            this.m_oDrawFilter.SetDropHighlight(null, ListViewDropLinePositionEnum.None);
        }


        void DragListView_DragLeave(object sender, EventArgs e)
        {
            // When the mouse goes outside the control, clear the drophighlight. 
            // Since the DropHighlight is cleared when the mouse is not over a ListViewItem, anyway, this is probably not needed.
            // But, just in case the user goes from a ListViewItem directly off the control...
            m_oDrawFilter.ClearDropHighlight();
        }

        void DragListView_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
        {
            // needed to change the drag drop to allow dropping
            e.Effect = DragDropEffects.Move;

            Point pointInListView = this.PointToClient(new Point(e.X, e.Y));

            UltraListViewItem item = this.ItemFromPoint(pointInListView);

            if (item == null)
            {
                // The Mouse is not over an item, do not allow dropping here
                e.Effect = DragDropEffects.None;

                // Erase any DropHightlight
                m_oDrawFilter.ClearDropHighlight();
            }
            else
            {
                e.Effect = DragDropEffects.Move;
                m_oDrawFilter.SetDropHighlight(item, pointInListView);
            }

            // Logic to scroll the WinListView if the cursor is dragged near the top or bottom edge of the control
            Point p = pointInListView;
            UltraListViewItem lvi = this.ItemFromPoint(p);

            if (p.Y < this.Height / 10)
            {
                Scroll(ScrollEventType.SmallDecrement);
            }
            if (p.Y > this.Height - (this.Height / 10))
            {
                Scroll(ScrollEventType.SmallIncrement);
            }
        }

        private void Scroll(ScrollEventType value)
        {
            Type listViewType = this.GetType();
            PropertyInfo scrollManagerProperty = listViewType.GetProperty("ScrollManager", BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance);            
            Object scrollManager = scrollManagerProperty.GetValue(this, null);
            Type scrollManagerType = scrollManager.GetType();
            scrollManagerType.InvokeMember("Scroll", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, scrollManager, new object[] { Orientation.Vertical, value });
        }

        protected void Scroll(UltraListViewItem lvi, int count)
        {
            KeyedSubObjectsCollectionBase list = this.Items;
            if (lvi.Group != null)
                list = lvi.Group.Items;
            int index = list.IndexOf(lvi);            
            if (index >= 0)
                index += count;

            if (index >= 0 && index < list.Count)
            {
                UltraListViewItem item = list.GetItem(index) as UltraListViewItem;
                if (item != null)
                    item.BringIntoView();
            }
        }

        void DragListView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            // get the item at the mouse location
            UltraListViewItem item = this.ItemFromPoint(new Point(e.X, e.Y));
            // if there is an item there, then we have to store for possible drag drop
            if (item != null)
            {
                // store the item
                this.itemToDrag = item;
                // set the flag so dragging can be done in the mouse move
                this.allowDrag = true;
                // store the start point
                this.startPoint = new Point(e.X, e.Y);
            }
        }

        void DragListView_MouseLeave(object sender, EventArgs e)
        {
            // if the mouse leaves the control, we will not be beginning a drag drop operation
            this.allowDrag = false;
        }

        void DragListView_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            // check if we have set the flag for allowing dragging (indicates that the mouse has gone 
            // down over an element and is still down and a drag drop hasn't yet started)
            if (this.allowDrag)
            {
                Point listViewPoint = new Point(e.X, e.Y);

                // check if we have moved a few pixels
                if (this.DragDistanceReached(this.startPoint, listViewPoint))
                {
                    // we have moved a vew pixels so we will begin the drag, AllowDrag flag is reset
                    this.allowDrag = false;
                    // Start the drag.
                    this.m_oDrawFilter.SetDropHighlight(this.itemToDrag, listViewPoint);
                    this.DoDragDrop(this.itemToDrag, DragDropEffects.Move);
                }
            }
        }

        void DragListView_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            // Once the mouse comes up, we no longer want to start a drag operation.
            this.allowDrag = false;
        }

        // determines if the mouse has moved a certain distance.
        private bool DragDistanceReached(Point p1, Point p2)
        {
            // get the x distance
            int xDist = Math.Abs(p1.X - p2.X);
            // get the y distance
            int yDist = Math.Abs(p1.Y - p2.Y);
            // get the total distance
            double totalDistance = Math.Sqrt(xDist * xDist + yDist * yDist);
            // we can drag if totalDistance is greater than three.
            return totalDistance > 3;
        }
    }
}
使用Infragistics.Win.UltraWinListView;
使用制度;
使用系统图;
使用System.Windows.Forms;
使用基础设施。共享;
运用系统反思;
命名空间Windows窗体应用程序1
{
公共类DragListView:UltraListView
{
私有DragListViewDrawFilter m_oDrawFilter=新的DragListViewDrawFilter();
//存储一个布尔值,该布尔值将确定鼠标何时移动,是否有要拖动的对象。
bool allowDrag=false;
//存储要拖动的项
UltraListViewItem itemToDrag=null;
//存储初始鼠标按下发生的点
点起点;
内部UltraListViewItemToDrag
{
获取{返回this.itemToDrag;}
}
受保护的重写void OnCreateControl()
{
base.OnCreateControl();
如果(!this.DesignMode)
{
//连接事件
this.DragDrop+=新系统.Windows.Forms.DragEventHandler(DragListView_DragDrop);
this.DragLeave+=新事件处理程序(DragListView\u DragLeave);
this.DragOver+=新系统.Windows.Forms.DragEventHandler(DragListView\u DragOver);
this.MouseDown+=新系统.Windows.Forms.MouseEventHandler(DragListView\u MouseDown);
this.MouseLeave+=新事件处理程序(DragListView_MouseLeave);
this.MouseMove+=new System.Windows.Forms.MouseEventHandler(DragListView\u MouseMove);
this.MouseUp+=新系统.Windows.Forms.MouseEventHandler(DragListView\u MouseUp);
this.m_oDrawFilter.Invalidate+=新事件处理程序(m_oDrawFilter_Invalidate);
//允许在控件上放置
this.AllowDrop=true;
m_oDrawFilter.DropLineColor=颜色.蓝色;
m_oDrawFilter.DropLineWidth=3;
this.DrawFilter=m_oDrawFilter;
}
}
void m_oDrawFilter_Invalidate(对象发送方,事件参数e)
{
这个。使无效();
}
void DragListView_DragDrop(对象发送方,System.Windows.Forms.DragEventArgs e)
{
//获取要放置到的项目(如果存在)(此事件的坐标是屏幕坐标,因此需要将其转换为客户端坐标)
UltraListViewItem dropTarget=m_oDrawFilter.DropHighlightItem;
//只有当我们有一个项目,我们正在下降,我们做什么
if(dropTarget!=null)
{
//如果dropLinePosition位于item之上,那么我们将把该项放在targetItems索引上
int indexModifier=0;
if(this.m_oDrawFilter.dropLinePosition==ListViewDropLinePositionEnum.BelowItem)
{
indexModifier=1;//被删除项的索引应该比targetItem多一个
}
UltraListViewItemToMove=e.Data.GetData(typeof(UltraListViewItem))作为UltraListViewItem;
if(itemToMove!=null)
{
//通过将项目从列表中删除并重新插入到添加到indexModifier的dropTarget的索引来移动该项目
此.Items.Remove(itemToMove);
this.Items.Insert(dropTarget.Index+indexModifier,itemToMove);
itemToMove.Group=dropTarget.Group;
}
}
此.m_oDrawFilter.SetDropHighlight(null,ListViewDropLinePositionEnum.None);
}
void DragListView\u DragLeave(对象发送方,事件参数e)
{
//当鼠标离开控件时,清除drophighlight。
//由于当鼠标不在ListViewItem上时,DropHighlight会被清除,因此可能不需要这样做。
//但是,万一用户从ListViewItem直接离开控件。。。
m_oDrawFilter.ClearDropHighlight();
}
void DragListView_DragOver(对象发送方,System.Windows.Forms.DragEventArgs e)
{
//需要更改拖放以允许拖放
e、 效果=DragDropEffects.Move;
Point pointInListView=this.PointToClient(新点(e.X,e.Y));
UltraListViewItem项=this.ItemFromPoint(pointInListView);
如果(项==null)
{
//鼠标不在项目上方,不允许将鼠标放在此处
e、 效果=DragDropEffects。无;
//清除掉任何投光灯
m_oDrawFilter.ClearDropHighlight();
}
其他的
{
e、 效果=DragDropEffects.Move;
m_oDrawFilter.SetDropHighlight(项目,pointInListView);
}
//如果光标拖动到控件的上边缘或下边缘附近,则滚动WinListView的逻辑
点p=点视图;
UltraListViewItem lvi=this.ItemFromPoint(p);
如果(p.Y<此高度/10)
{
滚动(ScrollEventType.smallDecreation);
}
如果(p.Y>此高度-(此高度/10))
{
滚动(ScrollEventType.SmallIncrement);
}
}
私有无效滚动(ScrollEventType值)
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infragistics.Win;
using Infragistics.Win.UltraWinListView;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    [System.Flags]
    internal enum ListViewDropLinePositionEnum
    {
        None = 0,
        AboveItem = 1,
        BelowItem = 2,
    }

    public class DragListViewDrawFilter : IUIElementDrawFilter
    {

        private UltraListViewItem dropHighlightItem = null;
        internal ListViewDropLinePositionEnum dropLinePosition { get; set; }

        public Color DropLineColor { get; set; }
        public int DropLineWidth { get; set; }

        public UltraListViewItem DropHighlightItem
        {
            get { return dropHighlightItem; }
            set
            {
                if (!this.dropHighlightItem.Equals(value))
                {
                    this.dropHighlightItem = value;
                    PositionChanged();
                }
            }
        }

        public event System.EventHandler Invalidate;

        #region IUIElementDrawFilter Members

        public bool DrawElement(DrawPhase drawPhase, ref UIElementDrawParams drawParams)
        {
            switch (drawPhase)
            {
                case DrawPhase.AfterDrawElement:
                    {
                        // We're in AfterDrawElement. So the only states we are conderned with are Below and Above
                        // Get the elemnt as an UltraListViewItemElement if it is one.
                        UltraListViewItemUIElement element = drawParams.Element as UltraListViewItemUIElement;
                        if (element != null && this.dropLinePosition != ListViewDropLinePositionEnum.None && this.dropHighlightItem == element.Item)
                        {
                            // Declare a pen to use for drawing Droplines
                            using (Pen p = new Pen(this.DropLineColor, this.DropLineWidth))
                            {
                                // Get a reference to the Grahpics object we are drawing to.
                                Graphics g = drawParams.Graphics;

                                // The left edge of the Dropline
                                int leftEdge = element.Rect.Left;
                                // right edge of the drop line
                                int rightEdge = element.Rect.Right;

                                int lineVPosition;

                                if (this.dropLinePosition == ListViewDropLinePositionEnum.BelowItem)
                                {
                                    // Draw Line below item
                                    lineVPosition = element.Rect.Bottom;
                                    g.DrawLine(p, leftEdge, lineVPosition, rightEdge, lineVPosition);
                                }
                                if (this.dropLinePosition == ListViewDropLinePositionEnum.AboveItem)
                                {
                                    // Draw Line below item
                                    lineVPosition = element.Rect.Top;
                                    g.DrawLine(p, leftEdge, lineVPosition, rightEdge, lineVPosition);

                                }
                            }
                        }
                        // Draw custom image where the mouse is when dragging.
                        UltraListViewUIElement listViewElement = drawParams.Element as UltraListViewUIElement;
                        if (null != listViewElement && this.dropLinePosition != ListViewDropLinePositionEnum.None)
                        {
                            DragListView dlv = listViewElement.Control as DragListView;

                            if (dlv != null && dlv.ItemToDrag != null)
                            {
                                Image img = dlv.ItemToDrag.Appearance.Image as Image;
                                if (img != null)
                                {
                                    Point cursorPosition = dlv.PointToClient(Cursor.Position);
                                    Rectangle rect = new Rectangle(cursorPosition, new Size(30, 30));
                                    drawParams.Graphics.DrawImage(img, rect);
                                }

                            }
                        }

                    }
                    break;
            }
            return false;
        }

        public DrawPhase GetPhasesToFilter(ref UIElementDrawParams drawParams)
        {
            return DrawPhase.AfterDrawElement;
        }

        #endregion

        internal void ClearDropHighlight()
        {
            this.SetDropHighlight(null, ListViewDropLinePositionEnum.None);
        }

        internal void SetDropHighlight(UltraListViewItem item, ListViewDropLinePositionEnum dropLinePosition)
        {
            //Use to store whether there have been any changes in 
            //DropItem or DropLinePosition
            bool IsPositionChanged = false;

            if (this.dropHighlightItem == null)
            {
                IsPositionChanged = !(item == null);
            }
            else
            {
                //Check to see if the items are equal and if 
                //the dropline position are equal
                if (this.dropHighlightItem.Equals(item) && (this.dropLinePosition == dropLinePosition))
                {
                    //They are both equal. Nothing has changed. 
                    IsPositionChanged = false;
                }
                else
                {
                    //One or both have changed
                    IsPositionChanged = true;
                }
            }
            //Set both properties without calling PositionChanged
            this.dropHighlightItem = item;
            this.dropLinePosition = dropLinePosition;

            //Check to see if the PositionChanged
            if (IsPositionChanged)
            {
                //Position did change.
                PositionChanged();
            }
        }


        /// <summary>
        /// When the dropItem or DropPosition change, we fire the Invalidate event to let the 
        /// program know to invalidate the Tree control. 
        /// This is neccessary since the DrawFilter does not have a reference to the Tree Control (although it probably could)
        /// </summary>
        private void PositionChanged()
        {
            // if nobody is listening then just return
            //
            if (null == this.Invalidate)
                return;

            EventArgs e = EventArgs.Empty;

            this.Invalidate(this, e);
        }

        internal void SetDropHighlight(UltraListViewItem item, Point pointInListView)
        {
            // The distance from the edge of the item used to determine whether to 
            // drop Above, Below
            int DistanceFromEdge = item.UIElement.Rect.Height / 2;

            // The new DropLinePosition
            ListViewDropLinePositionEnum NewDropLinePosition;

            // Determine which part of the item the point is in
            if (pointInListView.Y <= (item.UIElement.Rect.Top + DistanceFromEdge))
            {
                // Point is in the top of the oNode
                NewDropLinePosition = ListViewDropLinePositionEnum.AboveItem;
            }
            else
            {
                NewDropLinePosition = ListViewDropLinePositionEnum.BelowItem;
            }

            // Now that we have the new DropLinePosition, call the real proc to get things rolling
            SetDropHighlight(item, NewDropLinePosition);
        }
    }
}