如何将数据网格中绑定列中显示的文本格式化为函数的输出?
假设我的代码隐藏中有这样的功能:
Function Test(ByVal str As String) As String
Return Left(str, 5)
End Function
和一个像这样的数据网格:
<DataGrid Name="dg_Users" IsReadOnly="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="User ID" Binding="{Binding UserID}" />
<DataGridTextColumn Header="Username" Binding="{Binding Username}" />
</DataGrid.Columns>
</DataGrid>
绑定到 StronglyTyped IList。(这只是一个循环添加到强类型列表的 SqlCeDataReader 对象):
Private Shared Function Map(Of T As New)(ByVal _Rdr As IDataReader) As IList(Of T)
Try
Dim _t As Type = GetType(T)
Dim _en As New List(Of T)()
Dim _ht As New Hashtable()
Dim _props As PropertyInfo() = _t.GetProperties()
Parallel.ForEach(_props, Sub(info)
_ht(info.Name.ToUpper()) = info
End Sub)
While _Rdr.Read()
Dim newObject As New T()
For index As Integer = 0 To _Rdr.FieldCount - 1
Dim info As PropertyInfo = DirectCast(_ht(_Rdr.GetName(index).ToUpper()), PropertyInfo)
If (info IsNot Nothing) AndAlso info.CanWrite Then
info.SetValue(newObject, IsNull(Of Object)(_Rdr.GetValue(index), Nothing), Nothing)
End If
Next
_en.Add(newObject)
End While
_Rdr.Close()
Return _en
_ht.Clear() : _en.Clear()
Catch ex As Exception
Return Nothing
End Try
End Function
如何Username
使用上述功能格式化列Test
?