我有一个在XAML中定义的DataGrid
,用于显示绑定到我的ViewModel中的客户列表:
<DataGrid Name="CustomersDataGrid" ItemsSource="{Binding Customers}">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding showCustomerCommand}"
Content="{Binding Path=Name}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
网格的显示效果很好。我想显示单个客户的详细信息。之前,我已经为所选行设置了绑定,并在页面上绑定了一个按钮,该按钮绑定了以下命令:
RelayCommand _showCustomerCommand;
public ICommand showCustomerCommand
{
get
{
if (_showCustomerCommand == null)
{
_showCustomerCommand = new RelayCommand(param => this.ShowCustomer());
}
return _showCustomerCommand;
}
}
private void ShowCustomer()
{
if (Parent != null)
{
// Handle Customer Display Here
}
}
这个方法可以正常工作。但我想要能够单击行内的按钮,而不是基于所选行的单个按钮。我知道上面的XAML中数据上下文是错误的,但我不知道如何更正它,也不知道如何传递出发按钮按下的特定行。任何有关帮助我连接嵌入式按钮的建议都将不胜感激!