Файл MainWindow.xaml
<Window x:Class="WpfDataGrid.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:WpfDataGrid"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid AutoGenerateColumns="False"
Margin="10"
IsReadOnly="True"
ItemsSource="{Binding MyData}"
FontFamily="Gisha" FontSize="20">
<!-- color for selected row -->
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<!-- columns and rows -->
<DataGrid.Columns>
<!-- text column -->
<DataGridTextColumn Header="My Customer" Binding="{Binding Customer}" Width="150" />
<!-- text column -->
<DataGridTextColumn Header="My Item" Binding="{Binding Item}" Width="150"/>
<!-- text column -->
<DataGridTextColumn Header="My Price" Binding="{Binding Price}" Width="150"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
Файл MainWindow.xaml.cs
using System.Collections.ObjectModel;
using System.Windows;
namespace WpfDataGrid
{
public class MyShop
{
public string Customer { get; set; }
public string Item { get; set; }
public string Price { get; set; }
}
public partial class MainWindow : Window
{
public ObservableCollection<MyShop> MyData { get; set; }
public MainWindow()
{
InitializeComponent();
// for binding
this.DataContext = this;
// data for grid
MyData = new ObservableCollection<MyShop>()
{
new MyShop() {Customer="ACME", Item="Widget", Price="100"},
new MyShop() {Customer="ACME", Item="Widget", Price="100"},
new MyShop() {Customer="ACME", Item="Tyre", Price="200"},
new MyShop() {Customer="Amazon", Item="Pen", Price="50"},
new MyShop() {Customer="Amazon", Item="Paper", Price="20"},
new MyShop() {Customer="Autodesk", Item="CAD", Price="10"},
new MyShop() {Customer="Autodesk", Item="File", Price="40"},
new MyShop() {Customer="ABC", Item="Flowers", Price="503"},
new MyShop() {Customer="ABC", Item="Sand", Price="706"}
};
}
}
}