// 响应鼠标右键点击栏目标题的动作,出现上下文菜单 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); }
// 清除所有留存的排序信息,刷新list的标题栏上的陈旧的排序标志 public static void ClearSortColumns(ListView list) { ListViewProperty prop = GetListViewProperty(list); if (prop == null) { return; } prop.SortColumns.Clear(); SortColumns.ClearColumnSortDisplay(list.Columns); prop.CurrentDbName = ""; // 清除记忆 }
// 响应点击栏目标题的动作,进行排序 // 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; } }
// 响应选择标记发生变化的动作,修改栏目标题文字 // parameters: // protect_column_numbers 需要保护的列的列号数组。列号从0开始计算。所谓保护就是不破坏这样的列的标题,设置标题从它们以外的列开始算起。nRecPathColumn表示的列号不必纳入本数组,也会自动受到保护。如果不需要本参数,可以用null public static void OnSelectedIndexChanged(ListView list, int nRecPathColumn, List <int> protect_column_numbers) { ListViewProperty prop = GetListViewProperty(list); if (prop == null) { throw new Exception("ListView 的 Tag 没有包含 ListViewProperty 对象"); } if (list.SelectedItems.Count == 0) { // 清除所有栏目标题为1,2,3...,或者保留以前的残余值? return; } ListViewItem item = list.SelectedItems[0]; // 获得路径。假定都在第一列? string strRecPath = GetItemText(item, nRecPathColumn); ColumnPropertyCollection props = null; string strDbName = ""; if (String.IsNullOrEmpty(strRecPath) == true) { strDbName = "<blank>"; // 特殊的数据库名,表示第一列空的情况 props = prop.GetColumnName(strDbName); goto DO_REFRESH; } // 取出数据库名 strDbName = prop.ParseDbName(strRecPath); // GetDbName(strRecPath); if (String.IsNullOrEmpty(strDbName) == true) { return; } if (strDbName == prop.CurrentDbName) { return; // 没有必要刷新 } props = prop.GetColumnName(strDbName); DO_REFRESH: if (props == null) { // not found // 清除所有栏目标题为1,2,3...,或者保留以前的残余值? props = new ColumnPropertyCollection(); } // 修改文字 int index = 0; for (int i = 0; i < list.Columns.Count; i++) { ColumnHeader header = list.Columns[i]; if (i == nRecPathColumn) { continue; } // 越过需要保护的列 if (protect_column_numbers != null) { if (protect_column_numbers.IndexOf(i) != -1) { continue; } } #if NO if (index < props.Count) { if (header.Tag != null) { header.Tag = props[index]; } else { header.Text = props[index].Title; } } else { ColumnProperty temp = (ColumnProperty)header.Tag; if (temp == null) { header.Text = i.ToString(); } else { header.Text = temp.Title; } } #endif ColumnProperty temp = (ColumnProperty)header.Tag; if (index < props.Count) { if (temp != props[index]) { header.Tag = props[index]; temp = props[index]; } } else { temp = null; // 2013/10/5 多出来找不到定义的列,需要显示为数字 } if (temp == null) { // 如果 header 以前有文字就沿用,没有时才使用编号填充 2014/9/6 消除 BUG if (string.IsNullOrEmpty(header.Text) == true) { header.Text = i.ToString(); } } else { header.Text = temp.Title; } index++; } // 刷新排序列的显示。也就是说刷新那些参与了排序的个别列的显示 prop.SortColumns.RefreshColumnDisplay(list.Columns); prop.CurrentDbName = strDbName; // 记忆 }