自定义类。带有图像的DataGridView列文本
下午好!
有一个自定义类,将带有图像的文本列添加到DataGridView。
Public Class DataGridViewTextAndImageColumn
Inherits DataGridViewTextBoxColumn
Private imageValue As Image
Private imageSize_Renamed As Size
Public Sub New()
CellTemplate = New TextAndImageCell()
End Sub
Public Overrides Function Clone() As Object
Dim c As DataGridViewTextAndImageColumn = TryCast(MyBase.Clone(), DataGridViewTextAndImageColumn)
c.imageValue = imageValue
c.imageSize_Renamed = imageSize_Renamed
Return c
End Function
Public Property Image() As Image
Get
Return imageValue
End Get
Set(ByVal value As Image)
If Image IsNot value Then
imageValue = value
imageSize_Renamed = value.Size
If InheritedStyle IsNot Nothing Then
Dim inheritedPadding As Padding = InheritedStyle.Padding
DefaultCellStyle.Padding = New Padding(imageSize_Renamed.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
End If
End If
End Set
End Property
Friend ReadOnly Property ImageSize() As Size
Get
Return imageSize_Renamed
End Get
End Property
End Class
Public Class TextAndImageCell
Inherits DataGridViewTextBoxCell
Private imageValue As Image
Private imageSize As Size
Public Overrides Function Clone() As Object
Dim c As TextAndImageCell = TryCast(MyBase.Clone(), TextAndImageCell)
c.imageValue = imageValue
c.imageSize = imageSize
Return c
End Function
Public Property Image() As Image
Get
If OwningColumn Is Nothing OrElse Me.OwningTextAndImageColumn Is Nothing Then
Return imageValue
ElseIf imageValue IsNot Nothing Then
Return imageValue
Else
Return OwningTextAndImageColumn.Image
End If
End Get
Set(ByVal value As Image)
If imageValue IsNot value Then
imageValue = value
imageSize = value.Size
Dim inheritedPadding As Padding = InheritedStyle.Padding
Style.Padding = New Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
End If
End Set
End Property
Protected Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal cellState As DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
' Paint the base content
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
If Image IsNot Nothing Then
' Draw the image clipped to the cell.
Dim container As Drawing2D.GraphicsContainer = graphics.BeginContainer()
graphics.SetClip(cellBounds)
graphics.DrawImageUnscaled(Image, cellBounds.Location)
graphics.EndContainer(container)
End If
End Sub
Private ReadOnly Property OwningTextAndImageColumn() As DataGridViewTextAndImageColumn
Get
Return TryCast(OwningColumn, DataGridViewTextAndImageColumn)
End Get
End Property
End Class
表格:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With DataGridView1
.DefaultCellStyle.WrapMode = DataGridViewTriState.True
.Columns(1).DefaultCellStyle.WrapMode = DataGridViewTriState.True
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End With
End Sub
End Class
工作结果:
问题是:问题
是:
当列被线包装时,文本在单元格中垂直居中。如何在单元格中制作图片也以中心为中心,而不管线的高度如何?
Good afternoon!
There is a custom class that adds a text column with an image to the DataGridView.
Public Class DataGridViewTextAndImageColumn
Inherits DataGridViewTextBoxColumn
Private imageValue As Image
Private imageSize_Renamed As Size
Public Sub New()
CellTemplate = New TextAndImageCell()
End Sub
Public Overrides Function Clone() As Object
Dim c As DataGridViewTextAndImageColumn = TryCast(MyBase.Clone(), DataGridViewTextAndImageColumn)
c.imageValue = imageValue
c.imageSize_Renamed = imageSize_Renamed
Return c
End Function
Public Property Image() As Image
Get
Return imageValue
End Get
Set(ByVal value As Image)
If Image IsNot value Then
imageValue = value
imageSize_Renamed = value.Size
If InheritedStyle IsNot Nothing Then
Dim inheritedPadding As Padding = InheritedStyle.Padding
DefaultCellStyle.Padding = New Padding(imageSize_Renamed.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
End If
End If
End Set
End Property
Friend ReadOnly Property ImageSize() As Size
Get
Return imageSize_Renamed
End Get
End Property
End Class
Public Class TextAndImageCell
Inherits DataGridViewTextBoxCell
Private imageValue As Image
Private imageSize As Size
Public Overrides Function Clone() As Object
Dim c As TextAndImageCell = TryCast(MyBase.Clone(), TextAndImageCell)
c.imageValue = imageValue
c.imageSize = imageSize
Return c
End Function
Public Property Image() As Image
Get
If OwningColumn Is Nothing OrElse Me.OwningTextAndImageColumn Is Nothing Then
Return imageValue
ElseIf imageValue IsNot Nothing Then
Return imageValue
Else
Return OwningTextAndImageColumn.Image
End If
End Get
Set(ByVal value As Image)
If imageValue IsNot value Then
imageValue = value
imageSize = value.Size
Dim inheritedPadding As Padding = InheritedStyle.Padding
Style.Padding = New Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
End If
End Set
End Property
Protected Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal cellState As DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
' Paint the base content
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
If Image IsNot Nothing Then
' Draw the image clipped to the cell.
Dim container As Drawing2D.GraphicsContainer = graphics.BeginContainer()
graphics.SetClip(cellBounds)
graphics.DrawImageUnscaled(Image, cellBounds.Location)
graphics.EndContainer(container)
End If
End Sub
Private ReadOnly Property OwningTextAndImageColumn() As DataGridViewTextAndImageColumn
Get
Return TryCast(OwningColumn, DataGridViewTextAndImageColumn)
End Get
End Property
End Class
Form:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With DataGridView1
.DefaultCellStyle.WrapMode = DataGridViewTriState.True
.Columns(1).DefaultCellStyle.WrapMode = DataGridViewTriState.True
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End With
End Sub
End Class
Result of work:
The question is:
The question is:
When a column is line-wrapped, the text is centered vertically in the cell. How to make a picture in a cell also centered regardless of the line height?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论