<Window.Resources>
<local:IdToEnableConverter x:Key="IdToEnableConverter"/>
<local:DgRowBgColorConverter x:Key="DgRowBgColorConverter"/>
<Style x:Key="EditableCellStyle" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Cyan"/>
<Setter Property="IsReadOnly" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Id,Converter={StaticResource IdToEnableConverter}}"
Value="False">
<Setter Property="Background" Value="Gray"/>
<Setter Property="IsReadOnly" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<DataGridTextColumn Header="Id"
Binding="{Binding Id}" IsReadOnly="True"/>
<DataGridTextColumn Header="Name"
Binding="{Binding Name}"
EditingElementStyle="{StaticResource EditableCellStyle}"/>
![]()
![]()
![]()
![]()
//xaml
<Window x:Class="WpfApp30.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:WpfApp30"
WindowState="Maximized"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:IdToEnableConverter x:Key="IdToEnableConverter"/>
<local:DgRowBgColorConverter x:Key="DgRowBgColorConverter"/>
<Style x:Key="EditableCellStyle" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Cyan"/>
<Setter Property="IsReadOnly" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Id,Converter={StaticResource IdToEnableConverter}}"
Value="False">
<Setter Property="Background" Value="Gray"/>
<Setter Property="IsReadOnly" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<DataGrid
ItemsSource="{Binding BooksList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
VirtualizingPanel.IsVirtualizing="True"
AutoGenerateColumns="False">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Height" Value="100"/>
<Setter Property="FontSize" Value="50"/>
<Setter Property="Background" Value="Wheat"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Id,Converter={StaticResource DgRowBgColorConverter}}" Value="Blue" >
<Setter Property="Background" Value="Blue"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="Id"
Binding="{Binding Id}" IsReadOnly="True"/>
<DataGridTextColumn Header="Name"
Binding="{Binding Name}"
EditingElementStyle="{StaticResource EditableCellStyle}"/>
<DataGridTextColumn Header="Comment"
Binding="{Binding Comment}"
EditingElementStyle="{StaticResource EditableCellStyle}"/>
<DataGridTextColumn Header="ISBN"
Binding="{Binding ISBN}"
EditingElementStyle="{StaticResource EditableCellStyle}"/>
<DataGridTextColumn Header="Title"
Binding="{Binding Title}"
EditingElementStyle="{StaticResource EditableCellStyle}"/>
<DataGridTextColumn Header="Topic"
Binding="{Binding Topic}"
EditingElementStyle="{StaticResource EditableCellStyle}"/>
<DataGridTextColumn Header="Summary"
Binding="{Binding Summary}"
EditingElementStyle="{StaticResource EditableCellStyle}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
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 WpfApp30
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MainVM vm = new MainVM();
this.DataContext = vm;
}
}
public class MainVM : INotifyPropertyChanged
{
public MainVM()
{
InitBooks();
}
private void InitBooks()
{
BooksList = new ObservableCollection<Book>();
for(int i=0;i<10000;i++)
{
BooksList.Add(new Book()
{
Id = i + 1,
Name = $"Name_{i + 1}",
ISBN = $"ISBN_{Guid.NewGuid().ToString("N")}",
Title=$"Title_{i+1}",
Comment=$"Comment_{i+1}",
Summary=$"Summary_{i+1}",
Topic=$"Topic_{i+1}"
});
}
}
public event PropertyChangedEventHandler? PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var handler = PropertyChanged;
if (handler != null)
{
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
private ObservableCollection<Book> booksList;
public ObservableCollection<Book> BooksList
{
get
{
return booksList;
}
set
{
if (value != booksList)
{
booksList = value;
OnPropertyChanged();
}
}
}
}
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public string ISBN { get; set; }
public string Title { get; set; }
public string Topic { get; set; }
public string Summary { get; set; }
public string Comment { get; set; }
}
public class IdToEnableConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double dValue;
if (double.TryParse(value?.ToString(), out dValue))
{
return dValue % 5 == 0;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class DgRowBgColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double dValue;
if (double.TryParse(value?.ToString(), out dValue))
{
return dValue % 5 == 0?Brushes.Blue : Brushes.Gray;
}
return Brushes.Gray;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}