使用自动生成的列在WPF数据网格上显示图像
我的图像存储在MySQL数据库中,最近我从WinForms迁移到VB WPF 我已经通读了关于这个问题的问题,我不满意。 当自动生成列时,如何设置列以显示图像而不是字节数组 未指定列名,未指定列索引 如果我必须使用一个模板,有人能给我一个清晰的解释吗 请不要指向其他链接,因为我没有找到对我有用的答案 任何帮助都将不胜感激 我的网格上的其他信息:使用自动生成的列在WPF数据网格上显示图像,wpf,vb.net,Wpf,Vb.net,我的图像存储在MySQL数据库中,最近我从WinForms迁移到VB WPF 我已经通读了关于这个问题的问题,我不满意。 当自动生成列时,如何设置列以显示图像而不是字节数组 未指定列名,未指定列索引 如果我必须使用一个模板,有人能给我一个清晰的解释吗 请不要指向其他链接,因为我没有找到对我有用的答案 任何帮助都将不胜感激 我的网格上的其他信息: <Grid> <Grid.RowDefinitions> <RowDefiniti
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition />
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<Grid Grid.Row="1" Margin="0">
<DataGrid
x:Name="DataGrid1"
Grid.Column="0"
AutoGenerateColumns="True"
IsReadOnly="True"
ItemsSource="{Binding}"
SelectionMode="Extended"
SelectionUnit="Cell" />
</Grid>
</Grid>
我希望这个答案能帮助一些人,而不必使用转换器等 基本上,通过处理autogeneratingcolumn事件,可以设置和格式化值。假设数据库上的所有字节数组都是映像,否则应该实现额外的条件(columname等)
Private Sub DataGrid1_AutoGeneratingColumn(sender As Object, e As DataGridAutoGeneratingColumnEventArgs) Handles DataGrid1.AutoGeneratingColumn
Dim column As DataGridTextColumn = TryCast(e.Column, DataGridTextColumn)
If e.PropertyType = GetType(System.Byte()) Then
Try
Dim img As New FrameworkElementFactory(GetType(Image))
img.SetBinding(Image.SourceProperty, New Binding(e.PropertyName))
e.Column = New DataGridTemplateColumn With {
.CellTemplate = New DataTemplate() With {
.VisualTree = img
},
.Header = ""}
Catch ex As Exception
End Try
End If
If e.PropertyType = GetType(System.DateTime) Then
column.Binding.StringFormat = "dd/MM/yyyy hh:mm"
End If
If e.PropertyType = GetType(System.UInt32) Or e.PropertyType = GetType(System.Decimal) Then
column.Binding.StringFormat = "#,###0"
End If
End Sub