//xaml <Window.Resources> <local:SizeConverter x:Key="sizeConverter"/> <local:BooksData x:Key="booksData"/> </Window.Resources> <Grid> <DataGrid Grid.Row="1" x:Name="dg" ItemsSource="{StaticResource booksData}" CanUserAddRows="False" AutoGenerateColumns="False" SelectionMode="Extended"> <DataGrid.Columns> <DataGridTextColumn Header="Id" Binding="{Binding Id}"/> <DataGridTextColumn Header="ISBN" Binding="{Binding ISBN}"/> <DataGridTemplateColumn Header="Image"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Border Width="{Binding ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}, Converter={StaticResource sizeConverter}}" Height="{Binding ActualHeight,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}, Converter={StaticResource sizeConverter}}"> <Border.Background> <ImageBrush ImageSource="{Binding ImgSource}" Stretch="Uniform" /> </Border.Background> </Border> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid> public class BooksData : ObservableCollection<Book> { public BooksData() { var imgs = Directory.GetFiles(@"../../Images"); int imgsCount = imgs.Count(); for (int i = 0; i < 100000; i++) { this.Add(new Book() { Id = i + 1, ISBN = $"ISBN_{Guid.NewGuid().ToString("N")}", Name = $"Name_{i + 1}", Topic = $"Topic_{i + 1}", ImgUrl = $"{imgs[i % imgsCount]}", ImgSource = GetImageSource($"{imgs[i % imgsCount]}") }); } } private ImageSource GetImageSource(string url) { BitmapImage bmi = new BitmapImage(); bmi.BeginInit(); bmi.UriSource = new Uri(url, UriKind.RelativeOrAbsolute); bmi.EndInit(); if (bmi.CanFreeze) { bmi.Freeze(); } return bmi; } } public class Book { public int Id { get; set; } public string ISBN { get; set; } public string Name { get; set; } public string Topic { get; set; } public string ImgUrl { get; set; } public ImageSource ImgSource { get; set; } }
xaml
//xaml <Window x:Class="WpfApp27.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" WindowState="Maximized" xmlns:local="clr-namespace:WpfApp27" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <local:SizeConverter x:Key="sizeConverter"/> <local:BooksData x:Key="booksData"/> </Window.Resources> <Grid> <DataGrid Grid.Row="1" x:Name="dg" ItemsSource="{StaticResource booksData}" CanUserAddRows="False" AutoGenerateColumns="False" SelectionMode="Extended"> <DataGrid.Columns> <DataGridTextColumn Header="Id" Binding="{Binding Id}"/> <DataGridTextColumn Header="ISBN" Binding="{Binding ISBN}"/> <DataGridTemplateColumn Header="Image"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Border Width="{Binding ActualWidth,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}, Converter={StaticResource sizeConverter}}" Height="{Binding ActualHeight,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}, Converter={StaticResource sizeConverter}}"> <Border.Background> <ImageBrush ImageSource="{Binding ImgSource}" Stretch="Uniform" /> </Border.Background> </Border> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window> using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.IO; namespace WpfApp27 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } public class BooksData : ObservableCollection<Book> { public BooksData() { var imgs = Directory.GetFiles(@"../../Images"); int imgsCount = imgs.Count(); for (int i = 0; i < 100000; i++) { this.Add(new Book() { Id = i + 1, ISBN = $"ISBN_{Guid.NewGuid().ToString("N")}", Name = $"Name_{i + 1}", Topic = $"Topic_{i + 1}", ImgUrl = $"{imgs[i % imgsCount]}", ImgSource = GetImageSource($"{imgs[i % imgsCount]}") }); } } private ImageSource GetImageSource(string url) { BitmapImage bmi = new BitmapImage(); bmi.BeginInit(); bmi.UriSource = new Uri(url, UriKind.RelativeOrAbsolute); bmi.EndInit(); if (bmi.CanFreeze) { bmi.Freeze(); } return bmi; } } public class Book { public int Id { get; set; } public string ISBN { get; set; } public string Name { get; set; } public string Topic { get; set; } public string ImgUrl { get; set; } public ImageSource ImgSource { get; set; } } public class SizeConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { double d = 0; if (double.TryParse(value?.ToString(), out d)) { return d / 2; } return d; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }
标签:ItemsSource,referenced,get,bmi,System,set,using,WPF,public From: https://www.cnblogs.com/Fred1987/p/18523682