//xaml
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid ShowGridLines="True"
Width="{Binding DataContext.RowWidth,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</Grid.Resources>
<TextBlock Text="{Binding Id}" Grid.Row="0" Grid.Column="0"/>
<TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/>
<TextBlock Text="{Binding Title}" Grid.Row="0" Grid.Column="2"/>
<TextBlock Text="{Binding ISBN}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
//cs
private double rowWidth;
public double RowWidth
{
get
{
return rowWidth;
}
set
{
if(value!=rowWidth)
{
rowWidth = value;
OnPropertyChanged();
}
}
}
public class MainVM : INotifyPropertyChanged
{
private Window win;
private FrameworkElement fe;
public MainVM(Window winValue)
{
win=winValue;
if(win!=null)
{
win.Loaded+=Win_Loaded;
}
}
private void Win_Loaded(object sender, RoutedEventArgs e)
{
var tempFe = win.Content as FrameworkElement;
if(tempFe!=null)
{
fe=tempFe;
RowWidth = fe.ActualWidth;
InitBooksCollection();
}
}
}
![]()
//xaml
<Window x:Class="WpfApp2.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"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
WindowState="Maximized"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.IsContainerVirtualizable="True"
VirtualizingPanel.CacheLength="1"
VirtualizingPanel.CacheLengthUnit="Item"
VirtualizingPanel.VirtualizationMode="Recycling"
SnapsToDevicePixels="True"
CanUserAddRows="False"
AutoGenerateColumns="False">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Height" Value="300"/>
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="FontSize" Value="30"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Width" Value="{Binding DataContext.RowWidth,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="FontSize" Value="80"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid ShowGridLines="True"
Width="{Binding DataContext.RowWidth,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</Grid.Resources>
<TextBlock Text="{Binding Id}" Grid.Row="0" Grid.Column="0"/>
<TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/>
<TextBlock Text="{Binding Title}" Grid.Row="0" Grid.Column="2"/>
<TextBlock Text="{Binding ISBN}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
//cs
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
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;
namespace WpfApp2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var vm = new MainVM(this);
this.DataContext=vm;
}
}
public class MainVM : INotifyPropertyChanged
{
private Window win;
private FrameworkElement fe;
public MainVM(Window winValue)
{
win=winValue;
if(win!=null)
{
win.Loaded+=Win_Loaded;
}
}
private void Win_Loaded(object sender, RoutedEventArgs e)
{
var tempFe = win.Content as FrameworkElement;
if(tempFe!=null)
{
fe=tempFe;
RowWidth = fe.ActualWidth;
InitBooksCollection();
}
}
private void InitBooksCollection()
{
List<Book> booksList = new List<Book>();
for (int i = 0; i<100000; i++)
{
booksList.Add(new Book()
{
Id=i+1,
Name=$"Name_{i+1}",
Title=$"Title_{i+1}",
ISBN=$"ISBN_{Guid.NewGuid().ToString("N")}"
});
}
BooksCollection=new ObservableCollection<Book>(booksList);
}
public event PropertyChangedEventHandler? PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propName = "")
{
var handler = PropertyChanged;
if (handler!=null)
{
handler?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
private ObservableCollection<Book> booksCollection;
public ObservableCollection<Book> BooksCollection
{
get
{
return booksCollection;
}
set
{
if (value!=booksCollection)
{
booksCollection = value;
OnPropertyChanged(nameof(BooksCollection));
}
}
}
private double rowWidth;
public double RowWidth
{
get
{
return rowWidth;
}
set
{
if(value!=rowWidth)
{
rowWidth = value;
OnPropertyChanged();
}
}
}
}
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public string ISBN { get; set; }
public ImageSource ImgSource { get; set; }
}
}