예제 #1
0
        // 响应鼠标右键点击栏目标题的动作,出现上下文菜单
        public static void OnColumnContextMenuClick(ListView list,
                                                    ColumnClickEventArgs e)
        {
            int nClickColumn = e.Column;

            ListViewProperty prop = GetListViewProperty(list);

            if (prop == null)
            {
                throw new Exception("ListView的Tag没有包含ListViewProperty对象");
            }

#if NO
            ColumnSortStyle sortStyle = prop.GetSortStyle(nClickColumn);
            prop.SortColumns.SetFirstColumn(nClickColumn,
                                            sortStyle,
                                            list.Columns,
                                            true);
#endif
            ContextMenuStrip contextMenu = new ContextMenuStrip();

            ToolStripMenuItem menuItem    = null;
            ToolStripMenuItem subMenuItem = null;

            // list.Columns[nClickColumn].Text
            menuItem = new ToolStripMenuItem("设置排序方式");
            contextMenu.Items.Add(menuItem);

            ColumnSortStyle sortStyle = prop.GetSortStyle(list, nClickColumn);
            if (sortStyle == null)
            {
                sortStyle = ColumnSortStyle.None;
            }

            List <ColumnSortStyle> all_styles = prop.GetAllSortStyle(list, nClickColumn);

            foreach (ColumnSortStyle style in all_styles)
            {
                subMenuItem      = new ToolStripMenuItem();
                subMenuItem.Text = GetSortStyleCaption(style);
                SetSortStyleParam param = new SetSortStyleParam();
                param.ColumnIndex  = nClickColumn;
                param.prop         = prop;
                param.Style        = style;
                subMenuItem.Tag    = param;
                subMenuItem.Click += new EventHandler(menu_setSortStyle_Click);
                if (style == sortStyle)
                {
                    subMenuItem.Checked = true;
                }
                menuItem.DropDown.Items.Add(subMenuItem);
            }

            Point p = list.PointToClient(Control.MousePosition);
            contextMenu.Show(list, p);
        }
예제 #2
0
        // 响应点击栏目标题的动作,进行排序
        // parameters:
        //      bClearSorter    是否在排序后清除 sorter 函数
        public static void OnColumnClick(ListView list,
                                         ColumnClickEventArgs e,
                                         bool bClearSorter = true)
        {
            int nClickColumn = e.Column;

            ListViewProperty prop = GetListViewProperty(list);

            if (prop == null)
            {
                throw new Exception("ListView的Tag没有包含ListViewProperty对象");
            }

            // 2013/3/31
            // 如果标题栏没有初始化,则需要先初始化
            if (list.SelectedItems.Count == 0 && list.Items.Count > 0)
            {
                list.Items[0].Selected = true;
                OnSelectedIndexChanged(list,
                                       0,
                                       null);
                list.Items[0].Selected = false;
            }

            ColumnSortStyle sortStyle = prop.GetSortStyle(list, nClickColumn);

            prop.SortColumns.SetFirstColumn(nClickColumn,
                                            sortStyle,
                                            list.Columns,
                                            true);

            // 排序
            SortColumnsComparer sorter = new SortColumnsComparer(prop.SortColumns);

            if (prop.HasCompareColumnEvent() == true)
            {
                sorter.EventCompare += (sender1, e1) =>
                {
                    prop.OnCompareColumn(sender1, e1);
                };
            }
            list.ListViewItemSorter = sorter;

            if (bClearSorter == true)
            {
                list.ListViewItemSorter = null;
            }
        }