2016-12-28 49 views
1

我使用ObjectListView庫中的TreeListView組件。我使單元格值可編輯,當我雙擊他們TextBox將出現奇數偏移量。 我想刪除這個偏移量,但是如何?C#objectlistview在單元格編輯器中未定義的偏移量

開始編輯 enter image description here

正如你可以看到第一行,第二列( 「我的新設備」),TextBox後開始編輯 enter image description here

之前出現了偏移。

P.S.按預期編輯作品。只有膠印是令人討厭的我

P.P.S.正如你所看到的偏移量取決於第一列的偏移量。我如何將它改爲零? enter image description here

回答

2

經過長時間的搜索,我發現解決方案! 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區drawed。

此功能在源文件中追加偏移控制的範圍:

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.BoundsCellBounds值:

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