【发布时间】:2013-11-17 19:23:36
【问题描述】:
我有一个带有多个 Y 轴的 ZedGraph 控件,我希望允许用户更改 Y 轴的显示顺序。
不幸的是,GraphPane 的 YAxisList 对象上没有 Move 方法,就像它存在于 CurveList 上一样。
YAxisList 上的索引器是只读的。我不能使用the good old swap snippet 来交换列表中的轴。
我该如何继续?
【问题讨论】:
我有一个带有多个 Y 轴的 ZedGraph 控件,我希望允许用户更改 Y 轴的显示顺序。
不幸的是,GraphPane 的 YAxisList 对象上没有 Move 方法,就像它存在于 CurveList 上一样。
YAxisList 上的索引器是只读的。我不能使用the good old swap snippet 来交换列表中的轴。
我该如何继续?
【问题讨论】:
我实现了一个扩展方法,灵感来自CurveList 的Move 方法。
陷阱是现有曲线的 Y 轴索引需要相应更新。
public static int Move(this YAxisList list, GraphPane pane, int index, int relativePos)
{
if (index < 0 || index >= list.Count)
return -1;
var axis = list[index];
list.RemoveAt(index);
var newIndex = index + relativePos;
if (newIndex < 0)
newIndex = 0;
if (newIndex > list.Count)
newIndex = list.Count;
list.Insert(newIndex, axis);
foreach (var curve in pane.CurveList)
if (curve.YAxisIndex == newIndex)
curve.YAxisIndex = index;
else if (curve.YAxisIndex == index)
curve.YAxisIndex = newIndex;
return newIndex;
}
【讨论】: