XtraGrid:显示自定义行指示器图标
XtraGrid: Show custom Row Indicator icon
如何在 DevExpress XtraGrid 控件中显示自定义行指示器图标?
This example 演示在 RowIndicator 列中显示文本。但我想根据某些条件显示资源中的自定义 images/icons。
并且在 this example 中显示了错误图标。
这是我的代码:
Private cObj_ImageList As ImageList
cObj_ImageList = New ImageList()
cObj_ImageList.Images.Add(My.Resources.lock_red)
cObj_ImageList.Images.Add(My.Resources.unlock_blue)
Private Sub GridView_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs)
If e.Info.IsRowIndicator And e.RowHandle >= 0 Then
Try
For Each lObj_ChngdRow As ChangedRow In cObj_Lst_ChngdRows
If lObj_ChngdRow.CRRowHandle = e.RowHandle And lObj_ChngdRow.IsEditable Then
'e.Graphics.DrawIcon(cObj_RowStateUnlock, 0, 0)
'e.Graphics.DrawImageUnscaled(My.Resources.unlock_blue, 0, 0)
e.Info.ImageIndex = -1
e.Painter.DrawObject(e.Info)
Dim rec As Rectangle = e.Bounds
rec.Inflate(-1, -1)
Dim x1 As Integer = rec.X + (rec.Width - cObj_ImageList.ImageSize.Width) / 2
Dim y1 As Integer = rec.Y + (rec.Height - cObj_ImageList.ImageSize.Height) / 2
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(1), x1, y1)
e.Handled = True
End If
Next
Catch ex As Exception
End Try
End If
End Sub
这仅显示一行的图标。我想显示所有行。但是当点击按钮 Checkout 时,我想更改此图标。
编辑 1:
正如 nempoBu4 所解释的,我已经将我的代码修改得更简单了:
Private Sub GridView_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs)
Try
If e.Info.IsRowIndicator And e.RowHandle >= 0 Then
e.Handled = True
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(0), e.Bounds.X, e.Bounds.Y)
Dim lObj_ChngdRow = cObj_Lst_ChngdRows.Find(Function(item) item.CRRowHandle = e.RowHandle AndAlso item.IsEditable)
If Not lObj_ChngdRow Is Nothing Then
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(1), e.Bounds.X, e.Bounds.Y)
End If
End If
Catch ex As Exception
End Try
End Sub
见附件截图。
红色和蓝色图标彼此重叠。我想我必须清除 ImageList 然后再添加它们。
CustomDrawRowIndicator
事件是针对每一行单独触发的,因此您不需要对 cObj_Lst_ChngdRows
集合中的所有对象使用循环。在将对象添加到 cObj_Lst_ChngdRows
集合后,您还需要使行指示符无效。为此,您需要使用 GridView.InvalidateRowIndicator
方法。
这是示例:
Private Sub CheckoutButton_Click(sender As Object, e As EventArgs)
Dim rowHandle = gvException.FocusedRowHandle
Dim row = New ChangedRow()
row.CRRowHandle = rowHandle
row.IsEditable = True
cObj_Lst_ChngdRows.Add(row)
gvException.InvalidateRowIndicator(rowHandle)
End Sub
Private Sub GridView_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs)
If e.Info.IsRowIndicator AndAlso e.RowHandle >= 0 Then
Try
Dim lObj_ChngdRow = cObj_Lst_ChngdRows.Find(Function(item) item.CRRowHandle = e.RowHandle AndAlso item.IsEditable)
If Not lObj_ChngdRow Is Nothing Then
e.Info.ImageIndex = -1
e.Painter.DrawObject(e.Info)
Dim rec As Rectangle = e.Bounds
rec.Inflate(-1, -1)
Dim x1 As Integer = rec.X + (rec.Width - cObj_ImageList.ImageSize.Width) / 2
Dim y1 As Integer = rec.Y + (rec.Height - cObj_ImageList.ImageSize.Height) / 2
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(1), x1, y1)
e.Handled = True
End If
Catch ex As Exception
End Try
End If
End Sub
编辑 1
您正在调用 e.Graphics.DrawImageUnscaled
方法两次。您只需调用此方法一次。
这是示例:
Private Sub GridView_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs)
Try
If e.Info.IsRowIndicator And e.RowHandle >= 0 Then
e.Handled = True
Dim lObj_ChngdRow = cObj_Lst_ChngdRows.Find(Function(item) item.CRRowHandle = e.RowHandle AndAlso item.IsEditable)
Dim imageIndex As Integer
If lObj_ChngdRow Is Nothing Then
imageIndex = 0
Else
imageIndex = 1
End If
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(imageIndex), e.Bounds.X, e.Bounds.Y)
End If
Catch ex As Exception
End Try
End Sub
如何在 DevExpress XtraGrid 控件中显示自定义行指示器图标?
This example 演示在 RowIndicator 列中显示文本。但我想根据某些条件显示资源中的自定义 images/icons。
并且在 this example 中显示了错误图标。
这是我的代码:
Private cObj_ImageList As ImageList
cObj_ImageList = New ImageList()
cObj_ImageList.Images.Add(My.Resources.lock_red)
cObj_ImageList.Images.Add(My.Resources.unlock_blue)
Private Sub GridView_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs)
If e.Info.IsRowIndicator And e.RowHandle >= 0 Then
Try
For Each lObj_ChngdRow As ChangedRow In cObj_Lst_ChngdRows
If lObj_ChngdRow.CRRowHandle = e.RowHandle And lObj_ChngdRow.IsEditable Then
'e.Graphics.DrawIcon(cObj_RowStateUnlock, 0, 0)
'e.Graphics.DrawImageUnscaled(My.Resources.unlock_blue, 0, 0)
e.Info.ImageIndex = -1
e.Painter.DrawObject(e.Info)
Dim rec As Rectangle = e.Bounds
rec.Inflate(-1, -1)
Dim x1 As Integer = rec.X + (rec.Width - cObj_ImageList.ImageSize.Width) / 2
Dim y1 As Integer = rec.Y + (rec.Height - cObj_ImageList.ImageSize.Height) / 2
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(1), x1, y1)
e.Handled = True
End If
Next
Catch ex As Exception
End Try
End If
End Sub
编辑 1:
正如 nempoBu4 所解释的,我已经将我的代码修改得更简单了:
Private Sub GridView_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs)
Try
If e.Info.IsRowIndicator And e.RowHandle >= 0 Then
e.Handled = True
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(0), e.Bounds.X, e.Bounds.Y)
Dim lObj_ChngdRow = cObj_Lst_ChngdRows.Find(Function(item) item.CRRowHandle = e.RowHandle AndAlso item.IsEditable)
If Not lObj_ChngdRow Is Nothing Then
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(1), e.Bounds.X, e.Bounds.Y)
End If
End If
Catch ex As Exception
End Try
End Sub
见附件截图。
红色和蓝色图标彼此重叠。我想我必须清除 ImageList 然后再添加它们。
CustomDrawRowIndicator
事件是针对每一行单独触发的,因此您不需要对 cObj_Lst_ChngdRows
集合中的所有对象使用循环。在将对象添加到 cObj_Lst_ChngdRows
集合后,您还需要使行指示符无效。为此,您需要使用 GridView.InvalidateRowIndicator
方法。
这是示例:
Private Sub CheckoutButton_Click(sender As Object, e As EventArgs)
Dim rowHandle = gvException.FocusedRowHandle
Dim row = New ChangedRow()
row.CRRowHandle = rowHandle
row.IsEditable = True
cObj_Lst_ChngdRows.Add(row)
gvException.InvalidateRowIndicator(rowHandle)
End Sub
Private Sub GridView_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs)
If e.Info.IsRowIndicator AndAlso e.RowHandle >= 0 Then
Try
Dim lObj_ChngdRow = cObj_Lst_ChngdRows.Find(Function(item) item.CRRowHandle = e.RowHandle AndAlso item.IsEditable)
If Not lObj_ChngdRow Is Nothing Then
e.Info.ImageIndex = -1
e.Painter.DrawObject(e.Info)
Dim rec As Rectangle = e.Bounds
rec.Inflate(-1, -1)
Dim x1 As Integer = rec.X + (rec.Width - cObj_ImageList.ImageSize.Width) / 2
Dim y1 As Integer = rec.Y + (rec.Height - cObj_ImageList.ImageSize.Height) / 2
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(1), x1, y1)
e.Handled = True
End If
Catch ex As Exception
End Try
End If
End Sub
编辑 1
您正在调用 e.Graphics.DrawImageUnscaled
方法两次。您只需调用此方法一次。
这是示例:
Private Sub GridView_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs)
Try
If e.Info.IsRowIndicator And e.RowHandle >= 0 Then
e.Handled = True
Dim lObj_ChngdRow = cObj_Lst_ChngdRows.Find(Function(item) item.CRRowHandle = e.RowHandle AndAlso item.IsEditable)
Dim imageIndex As Integer
If lObj_ChngdRow Is Nothing Then
imageIndex = 0
Else
imageIndex = 1
End If
e.Graphics.DrawImageUnscaled(cObj_ImageList.Images(imageIndex), e.Bounds.X, e.Bounds.Y)
End If
Catch ex As Exception
End Try
End Sub