WPF Datagrid:使用基于数据源数值的转换器在列中显示图像
我有以下 XAML:
<Window.Resources>
<myApp:ImageDisplayConvertor x:Key="ImageDisplayConvertor" />
</Window.Resources>
<DataGridTemplateColumn Header="Icon">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<Image Name="img" Source="{Binding Game,Converter={StaticResource ImageDisplayConvertor}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
这是我的转换器代码:
Public Class ImageDisplayConvertor
Implements IValueConverter
Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Dim x As Integer = DirectCast(value, Integer)
Select Case x
Case 1 : Return My.Resources.T1
Case 2 : Return My.Resources.T2
Case 3 : Return My.Resources.T3_Blue
Case 10 : Return My.Resources.SS2
Case Else : Return My.Resources.imgError
End Select
End Function
Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Return value
End Function
End Class
返回的资源是位图。我知道转换器类正在触发,因为我已在消息框中发送了变量“x”,并且它显示了数据源“游戏”列中的整数值。但是,我的 TemplateColumn 仍然为空:
我做错了什么?如果我将图像源设置为静态 uri,我会在第一列中看到图像。但尝试绑定和转换是行不通的。
I have the following XAML:
<Window.Resources>
<myApp:ImageDisplayConvertor x:Key="ImageDisplayConvertor" />
</Window.Resources>
<DataGridTemplateColumn Header="Icon">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<Image Name="img" Source="{Binding Game,Converter={StaticResource ImageDisplayConvertor}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Here is my Converter code:
Public Class ImageDisplayConvertor
Implements IValueConverter
Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Dim x As Integer = DirectCast(value, Integer)
Select Case x
Case 1 : Return My.Resources.T1
Case 2 : Return My.Resources.T2
Case 3 : Return My.Resources.T3_Blue
Case 10 : Return My.Resources.SS2
Case Else : Return My.Resources.imgError
End Select
End Function
Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Return value
End Function
End Class
The resources returned are bitmaps. I know the converter class is firing because i've messagebox'd the variable 'x' and it displays the value of the integer from the 'Game' column of the datasource. However, my TemplateColumn remains empty:
What am I doing wrong? If I set the image source to a static uri, I see an image in the first column. But trying to bind and convert doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的转换器中,尝试返回带有位图 uri 的 BitmapImage,而不是位图本身:
In your converter, try returning a BitmapImage with the bitmap uri instead of the bitmap itself: