简体   繁体   中英

WPF - how to show in ICollectionView Bitmapimages?

So I have a List<Book> which contains a BitmapImage .

And I have a DataGrid which gets it's information from a ICollectionView which is initialized like that:

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;
        }

I previously used just a regular list of and bound it column by column to the DG_BooksList so I'll be able to manipulate the columns and it worked.

And when I wanted to create a filter for the list'I understood that using the ICollectionView might be a better way to go.

Only now I don't know how to manipulate each single column in the DataGrid When It's automatically giving it the information.

I tried to look for an answer but couldn't find, how do I manipulate single columns in the DataGrid like that and how do I show a BitmapImage in the DataGrid ?

If your Book type contains public property Logo and also implemented INotifyPropertyChanged.. you can use DataGridTemplateColumn and specify dataTemplate for it

<DataGridTemplateColumn Header="Logo">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Source="{Binding Logo}" Height="25" Width="25"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM