<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<DataGrid Name="dg" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="A">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<ContentPresenter Content="{Binding a}" HorizontalAlignment="Left" VerticalAlignment="Center" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="B + C">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter Content="{Binding b}" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Center" />
<Rectangle Grid.Row="1" Grid.ColumnSpan="2" Margin="0" Stroke="Black" StrokeThickness="1" />
<ContentPresenter Content="{Binding c}" Grid.Row="2" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Center" />
<Button Grid.Row="0" Grid.Column="1" Content="Edit" Margin="15,5,5,5" HorizontalAlignment="Right" Tag="{Binding}" Click="Button_Click" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Window>
This is the XAML code to use: the DataGrid has only 2 columns and the second one has a different template. I've used a grid to make 2 different rows in the second column. The Tag property of the button is bound to the object to edit, so, in the code behind, we can do this:
public struct Structure
{
public string a { get; set; }
public string b { get; set; }
public string c { get; set; }
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
dg.ItemsSource = new Structure[] { new Structure { a = "", b = "", c = "" }, new Structure { a = "1", b = "2", c = "3" } };
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Structure obj = (Structure)((Button)sender).Tag;
MessageBox.Show("Object to edit:\r\nA: " + obj.a + "\r\nB: " + obj.b + "\r\nC: " + obj.c);
}
}