Your problem is that DataColumns are not part of the visual tree and therefore do not inherit the DataContext DataGrid.
One way to potentially overcome this is to specify an ancestor with your binding:
<DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Content="Deliver Order" Command="{Binding Path=DataContext.DeliverPesananCommand ,RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate>
Another (slightly hacked) way is to declare a helper class that creates an attached property for the DataGridColumn class and then populates this property when the grid data type changes (it does this by handling the event changed at the FrameworkElement level and checking if the dependency object is responsible for the DataGrid event):
public class DataGridContextHelper { static DataGridContextHelper() { DependencyProperty dp = FrameworkElement.DataContextProperty.AddOwner(typeof(DataGridColumn)); FrameworkElement.DataContextProperty.OverrideMetadata( typeof(DataGrid) ,new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, OnDataContextChanged) ); } public static void OnDataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var grid = d as DataGrid; if (grid == null) return; foreach (var col in grid.Columns) { col.SetValue(FrameworkElement.DataContextProperty, e.NewValue); } } }
You can find more about this approach here .
source share