4

我在本地硬盘上有一个文件夹,里面有几张图片。图像名称/路径存储在本地 SQLCE 数据库中。在 WPF 应用程序中,我试图将这些图像绑定到 Image 元素(最终进入列表框)。我已经让应用程序运行和编译,并且列表框出现了,但是没有图像应该在哪里。

这是定义列表框使用的数据模板的 XAML...

<Window.Resources>
    <DataTemplate x:Key="assetLBTemplate">
        <StackPanel Orientation="Horizontal">
            <Image Height="32" Width="32" Source="{Binding imageFileName}" />
            <TextBlock Text="{Binding imageFileName}" />
            <TextBlock Text="{Binding assetName}" />
        </StackPanel>
    </DataTemplate>
</Window.Resources>

列表框的 XAML...

<ListBox x:Name="lbAssetsLiquid"
    ItemsSource="{Binding Tables[0]}"
    ItemTemplate="{StaticResource assetLBTemplate}"
    BorderThickness="1, 1, 1, 1" Grid.Column="0" Grid.Row="1" />

我在 Window_Loaded 上运行的代码:

    private void BindLiquidAssetsListBoxData()
    {
        SqlCeConnection connection;
        SqlCeCommand command;
        string sql = "SELECT tblLiquidAssets.assetName, tblLiquidAssets.assetQuantity, tblLiquidAssets.assetValueGP, tblLiquidAssets.assetDescription, tblImages.imageFileName FROM tblLiquidAssets INNER JOIN tblImages ON tblLiquidAssets.assetImageIndex=tblImages.imageID;";
        string connectionString = "Data Source=sharecalc_db.sdf;Persist Security Info=False;";
        DataSet dtSet = new DataSet();

        try
        {
            using (connection = new SqlCeConnection(connectionString))
            {
                command = new SqlCeCommand(sql, connection);
                SqlCeDataAdapter adapter = new SqlCeDataAdapter();
                connection.Open();
                adapter.SelectCommand = command;
                adapter.Fill(dtSet, "tblLiquidAssets");
                lbAssetsLiquid.DataContext = dtSet;
                connection.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

SQL 查询的结果是...

SQL 查询

再次...程序与列表框一起加载,但没有加载图像。

我在输出窗口中得到了这个,这让我觉得我在这里遗漏了一些重要的东西......

转换器无法转换值“gold64.png”(类型“字符串”)

当我在解决方案资源管理器中将图像添加到项目本身时,它似乎可以工作(图像出现在它们应该在的位置)......但否则它不起作用。有人可以将我推向正确的方向吗?

4

2 回答 2

2

如果要从文件系统加载文件,则需要使用自定义值转换器将字符串转换为图像。Image.Source,当传递一个字符串时,需要一个来自资源的文件名。您可以在此处找到此类转换器的实现:Display an image in WPF without hold the file open

于 2013-06-24T03:03:23.850 回答
2

谢谢Athari,你让我走上了正确的道路!

修改后的 XAML 块...

<Window x:Class="pf_sharecalc.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Share Calculator" WindowStyle="ThreeDBorderWindow" Loaded="Window_Loaded"
    xmlns:local="clr-namespace:pf_sharecalc">

<Window.Resources>
    <local:PathToImageConverter x:Key="PathToIMageConverter"/>
    <DataTemplate x:Key="assetLBTemplate">
        <StackPanel Orientation="Horizontal">
            <Image Height="32" Width="32" Source="{Binding imageFileName, Converter={StaticResource PathToIMageConverter}}" />
            <TextBlock Text="{Binding imageFileName}" />
            <TextBlock Text="{Binding assetName}" />
        </StackPanel>
    </DataTemplate>
</Window.Resources>

我添加了这段代码......

public class PathToImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string path = value as string;
        if (path != null)
        {
            BitmapImage image = new BitmapImage();
            using (FileStream stream = File.OpenRead(path))
            {
                image.BeginInit();
                image.StreamSource = stream;
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.EndInit(); // load the image from the stream
            } // close the stream
            return image;
        }
        else
            return null;
    }
    public object ConvertBack(object value, Type targetType,
                              object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

我必须做一些微调才能得到我想要的,但我已经通过了路障。

于 2013-06-24T03:49:19.487 回答