Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用从DB派生的相对文件路径将WPF图像元素绑定到本地硬盘上的PNG?_C#_Wpf_Image_Sql Server Ce - Fatal编程技术网

C# 如何使用从DB派生的相对文件路径将WPF图像元素绑定到本地硬盘上的PNG?

C# 如何使用从DB派生的相对文件路径将WPF图像元素绑定到本地硬盘上的PNG?,c#,wpf,image,sql-server-ce,C#,Wpf,Image,Sql Server Ce,我在本地硬盘上有一个文件夹,里面有几个图像。图像名称/路径存储在本地SQLCE数据库中。在WPF应用程序中,我试图将这些图像绑定到一个图像元素(最终进入列表框)。我已经运行并编译了应用程序,列表框显示了出来,但是没有它应该在的位置的图像 这是定义listbox使用的数据模板的XAML 列表框的XAML 我在Window_上运行的代码已加载: private void BindLiquidAssetsListBoxData() { SqlCeConnecti

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

这是定义listbox使用的数据模板的XAML


列表框的XAML


我在Window_上运行的代码已加载:

    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查询的结果是

再次…程序加载列表框,但没有加载图像

我在输出窗口中看到这个,这让我觉得我错过了一些重要的东西

转换器无法转换值“gold64.png”(类型“String”)


当我在解决方案资源管理器中将图像添加到项目本身时,它似乎可以工作(图像显示在它们应该位于的位置)…但如果不是这样,它就不工作了。有人能把我推到正确的方向吗?

如果要从文件系统加载文件,需要使用自定义值转换器将字符串转换为图像
Image.Source
,当传递字符串时,需要来自资源的文件名。您可以在这里找到这样一个转换器的实现:。

谢谢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>

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

“当传递字符串时,需要资源提供一个文件名”。对于绝对路径,情况并非如此。只需将
Source
属性绑定到包含图像文件绝对路径的字符串,无需转换器。WPF将通过其内置类型转换自动创建适当的ImageSource。可以通过添加try-catch来改进此转换器,因为路径可能不是会引发异常的图像。
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();
    }
}