In this sample I have two buttons. The button in the header works, but the one in the grid gives me the error show below.
<GroupBox DockPanel.Dock="Top" >
<GroupBox.Header>
<StackPanel Orientation="Horizontal">
<Label Content="Recent Servers" />
<CheckBox Content="Auto-Refresh" />
<Button Content="Monitor" Command="{Binding MonitorCommand, ElementName=Top}" CommandParameter="{Binding}" />
</StackPanel>
</GroupBox.Header>
<DataGrid CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False" ItemsSource="{Binding ServerHistory}" >
<DataGrid.Columns>
<DataGridTextColumn Header="Machine Name" Binding="{Binding DataPoints[ServerName], Mode=OneWay}" />
<DataGridTextColumn Header="Last Heartbeat" Binding="{Binding DataPoints[LastHeartbeat], Mode=OneWay}"/>
<DataGridTemplateColumn Header="Monitor">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Monitor" Command="{Binding MonitorCommand, ElementName=Top}" CommandParameter="{Binding}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</GroupBox>
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=Top'. BindingExpression:Path=MonitorCommand; DataItem=null; target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')