Файл 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">
<DataGrid.Columns>
<!-- text column -->
<DataGridTextColumn Header="Customer" Binding="{Binding Customer}" Width="150" />
<!-- text column -->
<DataGridTextColumn Header="Item" Binding="{Binding Item}" Width="150"/>
<!-- text column with vertical alignment=center -->
<DataGridTemplateColumn Header="Price" Width="120">
<!-- center data -->
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Price}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<!-- center header -->
<DataGridTemplateColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="Background" Value="Transparent"/>
</Style>
</DataGridTemplateColumn.HeaderStyle>
</DataGridTemplateColumn>
<!-- button -->
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding ClickMyButton1, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
CommandParameter="{Binding}"
Height="50"
Content="My button" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
C#
Файл MainWindow.xaml.cs
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
namespace WpfDataGrid
{
public class MyShop
{
public string Customer { get; set; }
public string Item { get; set; }
public string Price { get; set; }
}
class ButtonCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private Action<object> _callback;
public ButtonCommand(Action<object> callback)
{
_callback = callback;
}
public virtual void Execute(object parameter) { _callback(parameter); }
public virtual bool CanExecute(object parameter) { return true; }
}
public partial class MainWindow : Window
{
public ObservableCollection<MyShop> MyData { get; set; }
public ICommand ClickMyButton1 { 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"}
};
ClickMyButton1 = new ButtonCommand((parameter => {
MyShop myShop = parameter as MyShop;
}));
}
}
}