c# Datagrid / 使用 Josh Smith 的 RelayCommand class 将按钮与参数绑定?
c# Datagrid / Binding a button with Parameter using Josh Smith's RelayCommand class?
我一直在使用 Rachel 的解决方案将按钮与命令绑定:
现在我想在 DataGrid 中做同样的事情。
示例代码:
<DataGrid.Columns>
<DataGridTemplateColumn Header="CustomerID">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding CustomerId}"
Command="{Binding DataContext.DetailCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
与
private ICommand _detailCommand;
public ICommand DetailCommand
{
get
{
if (_detailCommand == null)
{
_detailCommand = new RelayCommand(
param => this.Execute(),
);
}
return _detailCommand;
}
}
private void Execute()
{
MessageBox.Show("Selected CustomerId : ");
}
这样做我可以调用命令,现在我的问题是如何将 CustomerId 属性 作为参数传递给 Execute() 方法?
好的,通过这个答案弄明白了:
只需要:
- 将param作为方法参数传递并更新签名
- 对我的按钮使用 CommandParameter
更新代码:
private ICommand _detailCommand;
public ICommand DetailCommand
{
get
{
if (_detailCommand == null)
{
_detailCommand = new RelayCommand(
param => this.Execute(param),
);
}
return _detailCommand;
}
}
private void Execute(object param)
{
MessageBox.Show($"Selected CustomerId : {param.ToString()}");
}
和
<DataGrid.Columns>
<DataGridTemplateColumn Header="CustomerID">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding CustomerId}"
Command="{Binding DataContext.DetailCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding CustomerId}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
我一直在使用 Rachel 的解决方案将按钮与命令绑定:
现在我想在 DataGrid 中做同样的事情。
示例代码:
<DataGrid.Columns>
<DataGridTemplateColumn Header="CustomerID">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding CustomerId}"
Command="{Binding DataContext.DetailCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
与
private ICommand _detailCommand;
public ICommand DetailCommand
{
get
{
if (_detailCommand == null)
{
_detailCommand = new RelayCommand(
param => this.Execute(),
);
}
return _detailCommand;
}
}
private void Execute()
{
MessageBox.Show("Selected CustomerId : ");
}
这样做我可以调用命令,现在我的问题是如何将 CustomerId 属性 作为参数传递给 Execute() 方法?
好的,通过这个答案弄明白了:
只需要:
- 将param作为方法参数传递并更新签名
- 对我的按钮使用 CommandParameter
更新代码:
private ICommand _detailCommand;
public ICommand DetailCommand
{
get
{
if (_detailCommand == null)
{
_detailCommand = new RelayCommand(
param => this.Execute(param),
);
}
return _detailCommand;
}
}
private void Execute(object param)
{
MessageBox.Show($"Selected CustomerId : {param.ToString()}");
}
和
<DataGrid.Columns>
<DataGridTemplateColumn Header="CustomerID">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding CustomerId}"
Command="{Binding DataContext.DetailCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding CustomerId}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>