C# objectlistview 单元格编辑器中 undefined offset

标签 c# textbox components objectlistview treelistview

我使用 ObjectListView 库中的 TreeListView 组件。我使单元格值可编辑,当我双击它们时 TextBox 将以奇数偏移量出现。 我想删除这个偏移量,但是如何删除呢?

开始编辑之前 enter image description here

开始编辑后 enter image description here

正如您所看到的第一行、第二列(“我的新设备”),TextBox 显示有偏移。

附注编辑工作按预期进行。只有偏移量让我很烦

P.P.S。正如您所看到的,偏移量取决于第一列偏移量。我怎样才能将其更改为零? enter image description here

最佳答案

经过长时间的搜索,我找到了解决方案!

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;
            }
        }
//...

关于C# objectlistview 单元格编辑器中 undefined offset ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41363661/

相关文章:

asp.net-core - Blazor 在组件之间传递 List<T>

c# - 你会如何简单地 Monitor.TryEnter

c# - Entity Framework 和 MySQL - 在没有 key 长度的 key 中使用 BLOB/TEXT 列

c# - 无法从配置部分 'common/logging' 获取 Common.Logging 的配置

jquery - 当文本框与标签位于不同的 div 中时水平对齐文本框

validation - 如何将一个错误提供程序应用于所有空文本框 VB.net 2010

android - React Native 0.57.x <Image/> 大图像低质量

c# - 我应该在哪里处理子表单

c# - 文本框填充

delphi - 将新组件拖放到表单上时出现错误消息 "Class Not Registered"