WPF Datagrid:使用基于数据源数值的转换器在列中显示图像

发布于 2024-12-01 00:56:14 字数 1590 浏览 1 评论 0原文

我有以下 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2024-12-08 00:56:14

在您的转换器中,尝试返回带有位图 uri 的 BitmapImage,而不是位图本身:

Dim bi As New BitmapImage
bi.BeginInit()
bi.UriSource = New Uri("yourBitmap.png", UriKind.Relative)
bi.EndInit()

In your converter, try returning a BitmapImage with the bitmap uri instead of the bitmap itself:

Dim bi As New BitmapImage
bi.BeginInit()
bi.UriSource = New Uri("yourBitmap.png", UriKind.Relative)
bi.EndInit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文