C# objectlistview 在单元格编辑器中未定义的偏移量
Posted
技术标签:
【中文标题】C# objectlistview 在单元格编辑器中未定义的偏移量【英文标题】:C# objectlistview undefined offset in cell editor 【发布时间】:2017-05-12 19:57:59 【问题描述】:我使用来自ObjectListView
库的TreeListView
组件。我使单元格值可编辑,当我双击它们时,TextBox
将出现奇数偏移。
我想移除这个偏移量,但是怎么做呢?
开始编辑之前
开始编辑后
如您所见,第一行第二列(“我的新设备”)TextBox
出现偏移。
附:按预期编辑工作。只有偏移量让我很烦
附言如您所见,偏移量取决于第一列偏移量。我怎样才能将其更改为零?
【问题讨论】:
【参考方案1】:经过长时间的搜索,我找到了解决方案!
OLV源码,ObjectListView.cs
public virtual void StartCellEdit(OLVListItem item, int subItemIndex)
OLVColumn column = this.GetColumn(subItemIndex);
Control c = this.GetCellEditor(item, subItemIndex);
Rectangle cellBounds = this.CalculateCellBounds(item, subItemIndex);
c.Bounds = this.CalculateCellEditorBounds(item, subItemIndex, c.PreferredSize);
// Try to align the control as the column is aligned. Not all controls support this property
Munger.PutProperty(c, "TextAlign", column.TextAlign);
// Give the control the value from the model
this.SetControlValue(c, column.GetValue(item.RowObject), column.GetStringValue(item.RowObject));
// Give the outside world the chance to munge with the process
this.CellEditEventArgs = new CellEditEventArgs(column, c, cellBounds, item, subItemIndex);
this.OnCellEditStarting(this.CellEditEventArgs);
if (this.CellEditEventArgs.Cancel)
return;
// The event handler may have completely changed the control, so we need to remember it
this.cellEditor = this.CellEditEventArgs.Control;
this.Invalidate();
this.Controls.Add(this.cellEditor);
this.ConfigureControl();
this.PauseAnimations(true);
我看到CellEditEventArgs
包含Control
,它在名为Bounds
的区域中绘制。
源文件中的这个函数将偏移量附加到控件的边界:
protected Rectangle CalculateCellEditorBoundsStandard(OLVListItem item, int subItemIndex, Rectangle cellBounds, Size preferredSize)
if (this.View == View.Tile)
return cellBounds;
// Center the editor vertically
if (cellBounds.Height != preferredSize.Height)
cellBounds.Y += (cellBounds.Height - preferredSize.Height) / 2;
// Only Details view needs more processing
if (this.View != View.Details)
return cellBounds;
// Allow for image (if there is one).
int offset = 0;
object imageSelector = null;
if (subItemIndex == 0)
imageSelector = item.ImageSelector;
else
// We only check for subitem images if we are owner drawn or showing subitem images
if (this.OwnerDraw || this.ShowImagesOnSubItems)
imageSelector = item.GetSubItem(subItemIndex).ImageSelector;
if (this.GetActualImageIndex(imageSelector) != -1)
offset += this.SmallImageSize.Width + 2;
// Allow for checkbox
if (this.CheckBoxes && this.StateImageList != null && subItemIndex == 0)
offset += this.StateImageList.ImageSize.Width + 2;
// Allow for indent (first column only)
if (subItemIndex == 0 && item.IndentCount > 0)
offset += (this.SmallImageSize.Width * item.IndentCount);
// Do the adjustment
if (offset > 0)
cellBounds.X += offset;
cellBounds.Width -= offset;
return cellBounds;
我们可以看到,附加到每个单元格的偏移量(不仅仅是第一个)。我们也可以看到,CellEditEventArgs
包含CellBounds
。
所以我们可以将Control.Bounds
重置为CellBounds
值:
//...
dataTreeView.CellEditStarting += DisableInputValueForCollections;
//...
private static void DisableInputValueForCollections(object sender, CellEditEventArgs e)
RemoveExtraOffsetForNotFirstColumnInputControl(e);
var node = e.RowObject as DataTreeNode;
if (node != null && e.Column.AspectName == "Value")
if (node.IsContainer()) e.Cancel = true;
//...
private static void RemoveExtraOffsetForNotFirstColumnInputControl(CellEditEventArgs e)
if (e.Column.AspectName != "Name")
e.Control.Bounds = e.CellBounds;
//...
【讨论】:
感谢您的帮助。 objectList 的奇怪行为。这是功能的错误? 我认为,它的错误。但最好问问开发人员【参考方案2】:该问题在 OLV v2.9.1 中仍然存在。我的解决方法与 muzagursiy 的做法略有不同。
olv.CellEditStarting += (sender, args) =>
// Left align the edit control
args.Control.Location = args.CellBounds.Location;
// Readjust the size of the control to fill the whole cell if CellEditUseWholeCellEffective is enabled
if (args.Column.CellEditUseWholeCellEffective)
args.Control.Size = args.CellBounds.Size;
;
【讨论】:
以上是关于C# objectlistview 在单元格编辑器中未定义的偏移量的主要内容,如果未能解决你的问题,请参考以下文章