我在 C# Net 2.0 Windows Forms 中有一个 ListView,其中大约有十列在应用程序启动时填充了数据。数据量很大,中间不能快速重新加载。我的 ListView 打开了详细信息 View ,并允许用户分别调整每一列的大小。
用户应该能够一次隐藏十列中的任何一列或其中的多列,并在非特定行中随时再次取消隐藏这些列。隐藏列时数据不会被删除。
以下是我尝试过但结果并不令人满意的事情:
1) 调整大小为 0 的列将使其稍微消失,直到用户开始播放 与列。用户不小心将它们重新调整大小,因为在我的 ListView 中有允许用户手动调整每列大小的选项。
2) 仅删除列会出现以下问题:当我尝试将列添加回去时,该列不记得它的位置和大小。位置是主要问题,我将尝试解释原因。如果我的用户决定先隐藏“第 2 列”,然后隐藏“第 3 列”,然后用户在第 2 列之前取消隐藏第 3 列,则“索引 2”不存在,因此我无法在索引号 3 处插入,并且出现异常。即使我记得删除前的索引位置,我也不能简单地将列放回该索引,因为我不知道以前的列是否也已经隐藏或者前后缺少或隐藏的列。 所以场景可能是这样的:显示 1 个,隐藏 2 个,也隐藏 3 个,显示 4 个,隐藏 5 个,隐藏 6 个,隐藏 7 个,显示 8 个,隐藏 9 个,隐藏 10 个。
场景中自动排除了可能的解决方案“1)”和“2)”。 更好的是将列的宽度设为零,但因为我的用户应该能够调整大小 根据需要随时列,将大小调整为零,它不能对用户隐藏。用户将通过调整大小取消隐藏它,我的系统会认为它仍然隐藏等等,如果隐藏的列可以只是“调整大小”或者我们如何命名它看起来不专业。
有人有更好的主意吗?我想知道为什么 listView 列没有“可见”或“隐藏”属性? 如果之前有人这样做过,请发布解决方案。
我必须补充一点,当添加数据时,我会自动调整 ListView 中所有列的大小。因此,下面的答案不起作用。调整大小事件无法检测到 -1 的宽度。添加数据时,宽度为 0 的“所有不可见列”的大小将重新调整。由于 ListView 删除了与列长度重叠的数据,因此我需要永久自动调整它的大小。资源管理器没有这个问题,因为它使列适合数据的长度。 C#没有这么高级的 ListView ,这里我每次添加数据时都必须将列设置为-1。在这个概念中,用于隐藏列的 column.width = 0 的想法不起作用。
最佳答案
好的,您的问题实际上是How to hide a ListView Column?。网上很多人都问过这个问题。我试过搜索很多但找不到任何东西。我最终得到了唯一的解决方案:将列宽设置为零。这将在这里使用一些技巧:
//This will try hiding the column at index 1
listView1.Columns[1].Width = 0;
//ColumnWidthChanging event handler of your ListView
private void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e){
if(e.ColumnIndex == 1){
e.Cancel = true;
e.NewWidth = 0;
}
}
它几乎完美地工作。但是,当用户将鼠标移到隐藏列位置的 pipe
上时,会出现一个 Cursor
指示符,以通知用户类似 There is a Zero-width此处的列,只需按住鼠标并拖动即可调整其大小。当然,用户无法从零 调整它的大小,因为我们取消
它并使NewWidth = 0
(如上面的代码那样)。但是 Cursor
通知这样的操作让它有点讨厌,这里是演示问题的屏幕截图:
要解决这个问题并不容易。至少那是我的感觉。我想到了这个似乎工作正常的解决方案。这个想法是我们必须检测鼠标是否在隐藏列的管道附近,我们必须设置 Cursor = Cursors.Arrow
。这是我认为对你有用的整个类(class):
public class CustomListView : ListView
{
[DllImport("user32")]
private static extern bool EnumChildWindows(IntPtr parentHwnd, EnumChildProc proc, object lParam);
delegate bool EnumChildProc(IntPtr childHwnd, object lParam);
public CustomListView()
{
VisibleChanged += (s, e) =>
{
if (Visible && headerHandle == IntPtr.Zero&&!DesignMode)
{
EnumChildWindows(Handle, EnumChild, null);
headerProc = new HeaderProc(this);
headerProc.AssignHandle(headerHandle);
}
};
columnPipeLefts[0] = 0;
}
//Save the Handle to the Column Headers, a ListView has only child Window which is used to render Column headers
IntPtr headerHandle;
//This is used use to hook into the message loop of the Column Headers
HeaderProc headerProc;
private bool EnumChild(IntPtr childHwnd, object lParam)
{
headerHandle = childHwnd;
return true;
}
//Updated code
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x101e&&hiddenColumnIndices.Contains(m.WParam.ToInt32()))//WM_SETCOLUMNWIDTH = 0x101e
{
if(m.LParam.ToInt32() > 0) hiddenColumnWidths[m.WParam.ToInt32()] = m.LParam.ToInt32();
return;//Discard the message changing hidden column width so that it won't be shown again.
}
base.WndProc(ref m);
}
//Save the column indices which are hidden
List<int> hiddenColumnIndices = new List<int>();
//Save the width of hidden columns
Dictionary<int, int> hiddenColumnWidths = new Dictionary<int, int>();
//Save the Left (X-Position) of the Pipes which separate Column Headers.
Dictionary<int, int> columnPipeLefts = new Dictionary<int, int>();
protected override void OnColumnWidthChanging(ColumnWidthChangingEventArgs e)
{
if (hiddenColumnIndices.Contains(e.ColumnIndex))
{
e.Cancel = true;
e.NewWidth = 0;
}
base.OnColumnWidthChanging(e);
}
//We need to update columnPipeLefts whenever the width of any column changes
protected override void OnColumnWidthChanged(ColumnWidthChangedEventArgs e)
{
base.OnColumnWidthChanged(e);
UpdateColumnPipeLefts(Columns[e.ColumnIndex].DisplayIndex + 1);
}
int index = -1;
protected override void OnColumnReordered(ColumnReorderedEventArgs e)
{
int i = Math.Min(e.NewDisplayIndex, e.OldDisplayIndex);
index = index != -1 ? Math.Min(i + 1, index) : i + 1;
base.OnColumnReordered(e);
}
//This is used to update the columnPipeLefts every reordering columns or resizing columns.
private void UpdateColumnPipeLefts(int fromIndex)
{
int w = fromIndex > 0 ? columnPipeLefts[fromIndex - 1] : 0;
for (int i = fromIndex; i < Columns.Count; i++)
{
w += i > 0 ? Columns.OfType<ColumnHeader>().Where(k=>k.DisplayIndex == i - 1).Single().Width : 0;
columnPipeLefts[i] = w;
}
}
//This is used to hide a column with ColumnHeader passed in
public void HideColumn(ColumnHeader col)
{
if (!hiddenColumnIndices.Contains(col.Index))
{
hiddenColumnWidths[col.Index] = col.Width;//Save the current width to restore later
col.Width = 0;//Hide the column
hiddenColumnIndices.Add(col.Index);
}
}
//This is used to hide a column with column index passed in
public void HideColumn(int columnIndex)
{
if (columnIndex < 0 || columnIndex >= Columns.Count) return;
if (!hiddenColumnIndices.Contains(columnIndex))
{
hiddenColumnWidths[columnIndex] = Columns[columnIndex].Width;//Save the current width to restore later
Columns[columnIndex].Width = 0;//Hide the column
hiddenColumnIndices.Add(columnIndex);
}
}
//This is used to show a column with ColumnHeader passed in
public void ShowColumn(ColumnHeader col)
{
hiddenColumnIndices.Remove(col.Index);
if(hiddenColumnWidths.ContainsKey(col.Index))
col.Width = hiddenColumnWidths[col.Index];//Restore the Width to show the column
hiddenColumnWidths.Remove(col.Index);
}
//This is used to show a column with column index passed in
public void ShowColumn(int columnIndex)
{
if (columnIndex < 0 || columnIndex >= Columns.Count) return;
hiddenColumnIndices.Remove(columnIndex);
if(hiddenColumnWidths.ContainsKey(columnIndex))
Columns[columnIndex].Width = hiddenColumnWidths[columnIndex];//Restore the Width to show the column
hiddenColumnWidths.Remove(columnIndex);
}
//The helper class allows us to hook into the message loop of the Column Headers
private class HeaderProc : NativeWindow
{
[DllImport("user32")]
private static extern int SetCursor(IntPtr hCursor);
public HeaderProc(CustomListView listView)
{
this.listView = listView;
}
bool mouseDown;
CustomListView listView;
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x200 && listView!=null && !mouseDown)
{
int x = (m.LParam.ToInt32() << 16) >> 16;
if (IsSpottedOnAnyHiddenColumnPipe(x)) return;
}
if (m.Msg == 0x201) {
mouseDown = true;
int x = (m.LParam.ToInt32() << 16) >> 16;
IsSpottedOnAnyHiddenColumnPipe(x);
}
if (m.Msg == 0x202) mouseDown = false;
if (m.Msg == 0xf && listView.index != -1 && MouseButtons == MouseButtons.None) { //WM_PAINT = 0xf
listView.UpdateColumnPipeLefts(listView.index);
listView.index = -1;
};
base.WndProc(ref m);
}
private bool IsSpottedOnAnyHiddenColumnPipe(int x)
{
foreach (int i in listView.hiddenColumnIndices.Select(j=>listView.Columns[j].DisplayIndex))
{
if (x > listView.columnPipeLefts[i] - 1 && x < listView.columnPipeLefts[i] + 15)
{
SetCursor(Cursors.Arrow.Handle);
return true;
}
}
return false;
}
}
}
关于c# - 在不删除列或将其大小调整为零的情况下隐藏详细信息 View 中的 Windows 窗体 ListView 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18110773/