自定义类。带有图像的DataGridView列文本

发布于 2025-02-07 02:35:17 字数 4658 浏览 3 评论 0原文

下午好!

有一个自定义类,将带有图像的文本列添加到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:

enter image description here

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?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文