【发布时间】:2015-05-30 00:08:11
【问题描述】:
我仍然很想了解 MVVM。 我一直在显示我的图像,哪些路径存储在 observableCollection 中。过去我可以在视图中使用代码,但现在当我尝试在我的项目上实现 MVVM 时,它不再显示任何图像。
这是我在视图模型中的代码
private ObservableCollection<Image> _selectedImageList = new ObservableCollection<Image>();
public ObservableCollection<Image> SelectedImageList
{
get { return _findImageList; }
set { _findImageList = value; }
}
public ImageLibraryViewModel()
{
string dbConnectionString = @"Data Source =movieprepper.sqlite;";
SQLiteConnection cnn = new SQLiteConnection(dbConnectionString);
cnn.Open();
string Query = "Select* from images";
SQLiteDataAdapter sda = new SQLiteDataAdapter(Query, cnn);
DataTable dt = new DataTable();
sda.Fill(dt);
cnn.Close();
foreach (DataRow row in dt.Rows)
{
string path = AppDomain.CurrentDomain.BaseDirectory.Substring(0, (AppDomain.CurrentDomain.BaseDirectory.Length - 10));
string name = row["path"].ToString();
Uri uri = new Uri(path + name);
Image I = new Image(uri.ToString(), row["path"].ToString(), row["title"].ToString());
SelectedImageList.Add(I);
}
}
这就是我在 XAML 中的内容
<ListBox ItemsSource="{Binding SelectedImageList}" dd:DragDrop.IsDropTarget="True">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Thumbnail}" Width="200"/>
<TextBox Text="{Binding Thumbnail}"></TextBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在绑定到缩略图的文本中,我可以清楚地读取图像指向的路径,但图像不会显示在列表中。在过去没有 MVVM 的情况下,Image Source 会自动将文件路径转换为图像。有人知道我哪里出错了吗?感谢您的帮助。
【问题讨论】:
-
如果您分享如何在 Image 类上定义 Thumbnail 属性,可能会有所帮助。它是什么类型的?此外,在您的问题案例中,它从它的 getter 返回什么值?我在提供的代码中看不到任何会导致您所描述的问题的内容。
-
BaseDirectory.Substring(0, (AppDomain.CurrentDomain.BaseDirectory.Length - 10))是不是要切断当前目录的“bin\Debug\”部分?你不应该那样做。发布版本是什么,它是“bin\Release\”,或者当应用程序以完全不同的路径安装时?图片的根文件夹可能是您的应用程序的配置设置。