c# - WPF - 如何在 ICollectionView 位图图像中显示?
问题描述
所以我有一个List<Book>
包含一个BitmapImage
.
我有一个从一个这样初始化的DataGrid
它获取它的信息:ICollectionView
ObservableCollection<Book> OCBooks = new ObservableCollection<Book>();
CollectionViewSource BooksCollection;
Predicate<object> yourCostumFilter;
ICollectionView Itemlist;
Book b = Book.Instance;
. . .
private void InitializeObservableCollection()
{
foreach (var item in ChangeBooksImagesToBitmapImage(b.GetBooksList()))
OCBooks.Add(item);
BooksCollection = new CollectionViewSource { Source = OCBooks };
Itemlist = BooksCollection.View;
DG_BooksList.ItemsSource = Itemlist;
}
. . .
private List<Book> ChangeBooksImagesToBitmapImage(List<Book> list)
{
// takes a Books list with imgURLs and returns one with BitmapImage
List<Book> newbl = new List<Book>();
foreach (var book in list)
{
if (!string.IsNullOrEmpty(book.ImgURL))
{
BitmapImage logo = new BitmapImage();
logo.BeginInit();
logo.UriSource = new Uri(book.ImgURL);
logo.DecodePixelHeight = 25;
logo.DecodePixelWidth = 25;
logo.EndInit();
newbl.Add(
new Book(
book.BookName,
book.Genre,
DateTime.Parse(book.Published.ToShortDateString()),
book.Publisher,
logo,
book.Quantity));
}
else
newbl.Add(book);
}
return newbl;
}
我以前只使用了一个常规列表并将其逐列绑定到 ,DG_BooksList
因此我将能够操作这些列并且它可以工作。
当我想为列表创建过滤器时,我明白使用ICollectionView
可能是更好的方法。
DataGrid
只是现在我不知道如何操作When It's automatically give it the information中的每一列。
我试图寻找答案但找不到,我如何DataGrid
像这样操作单列以及如何BitmapImage
在DataGrid
?
解决方案
如果您的 Book 类型包含公共属性 Logo 并且还实现了 INotifyPropertyChanged.. 您可以使用 DataGridTemplateColumn 并为其指定 dataTemplate
<DataGridTemplateColumn Header="Logo">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Logo}" Height="25" Width="25"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
推荐阅读
- ssl - 在 nginx 中记录 SSL 握手失败原因以及客户端 IP
- python - How to output dictionary to dataframe including None type values
- mongodb - Spring Boot、MongDB 和 Kotlin
- java - JavaFX:使用 AnimationTimer 移动矩形
- c - 不要从函数中获取变量的值
- node.js - 如何搜索所有字段,包括部分查询文本?
- javascript - 如何通过 React 操作创建器在 Firebase 中创建嵌套数据库?
- c++ - 在运行时猜测函数的返回类型
- node.js - Sequelize:将日期存储在 JSON 列中,并将它们与 where 子句中的日期进行比较
- python-3.x - 如何在 x 轴上设置带有日期的图表