[英]Create custom generic list with bitmapimage
In my WPF project there is a listbox in which I have to display images and next to each image their text (for example: date the photo was taken, location etc).在我的 WPF 项目中,有一个列表框,我必须在其中显示图像并在每个图像旁边显示它们的文本(例如:照片的拍摄日期、位置等)。 I have tried creating a generic List but I still can't assign it to the listbox
我已经尝试创建一个通用列表,但我仍然无法将它分配给列表框
Something like就像是
Bscially I have been trying something on this lines. Bscially 我一直在尝试这方面的东西。
public class LoadImages
{
public static List<ImageLoader> LoadImages()
{
List<ImageLoader> img = new List<ImageLoader>();
Uri uri = new Uri(@"http://somedomain.com/pic.jpg", UriKind.Absolute);
BitmapImage bi = new BitmapImage(uri);
img.Add(new ImageLoader("1_1",bi));
return img;
}
}
public class ImageLoader
{
string mediaid;
BitmapImage thumbnail;
public ImageLoader(string mediaid, BitmapImage b)
{
this.mediaid = mediaid;
this.thumbnail = b;
}
}
And my XAML looks like this.而我的 XAML 看起来像这样。
<ListBox Name="ListBox1" SelectionMode="Extended" ItemsSource="{Binding}"
Width="300" Height="300" IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Image Source="{Binding thumbnail}"/>
<TextBlock Text="{Binding mediaid}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
For the time being I have set the Window.DataContext暂时我已经设置了 Window.DataContext
<Window.DataContext>
<ObjectDataProvider ObjectType="{x:Type local:LoadImages}" MethodName="LoadImages"/>
</Window.DataContext>
But everytime I run the app the listbox shows up empty.但是每次我运行应用程序时,列表框都会显示为空。
Any suggestions.有什么建议么。
Image element do support Uris for Source property.图像元素确实支持源属性的 Uris。 Why not making your LoadImages class return a set of Uris instead of images?
为什么不让您的 LoadImages class 返回一组 Uris 而不是图像? Also image element can do async job for you;)
图像元素也可以为你做异步工作;)
thumbnail and mediaid aren't public, and thus the binding fails. thumbnail 和 mediaid 不公开,因此绑定失败。
After a lot of reading and googling I found my answer, all I had to do in the ImageLoader class is to create properties for mediaid and thumbnail and after that binding to the listbox now works like a char.经过大量阅读和谷歌搜索后,我找到了答案,我在 ImageLoader class 中要做的就是为 mediaid 和缩略图创建属性,然后绑定到列表框现在就像一个字符一样工作。 So the ImageLoader class in asked in the question above now looks like
所以上面问题中提出的 ImageLoader class 现在看起来像
public class ImageLoader
{
string mediaid;
BitmapImage thumbnail;
public string MediaId
{
get { return mediaid; }
set { mediaid = value; }
}
public BitmapImage Thumbnail
{
get { return thumbnail; }
set { thumbnail = value; }
}
public ImageLoader(string mediaid, BitmapImage b)
{
this.mediaid = mediaid;
this.thumbnail = b;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.