我在本地硬盘上有一个文件夹,里面有几张图片。图像名称/路径存储在本地 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 查询的结果是...
再次...程序与列表框一起加载,但没有加载图像。
我在输出窗口中得到了这个,这让我觉得我在这里遗漏了一些重要的东西......
转换器无法转换值“gold64.png”(类型“字符串”)
当我在解决方案资源管理器中将图像添加到项目本身时,它似乎可以工作(图像出现在它们应该在的位置)......但否则它不起作用。有人可以将我推向正确的方向吗?