所以我最初的想法已經足夠接近了。
當我將CommandParameter綁定到DataGrid時,問題是,當綁定被解析時,DataGrid不知道什麼是CurrentColumn或CurrentCell或CurrentItem,所以它解析爲空值。
因此,我改變綁定綁定到DataGridCell,而不是問題解決了 - Cell有能力在綁定解析時告訴它的屬於它的列和項,所以當命令被觸發時,它已經擁有了所有正確的數據。
風格一直在尋找這樣的事情:
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},
Path=DataContext[RowActionFeature].RowActionCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridCell}},
Converter={StaticResource DataGridCellToRowActionParametersConverter}}">
...
</Button>
器和轉換器是這樣的:
public class DataGridCellToRowActionParametersConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var dataGridCell = value as DataGridCell;
if (dataGridCell == null)
{
return null;
}
var dataRowView = dataGridCell.DataContext as DataRowView;
var columnIndex = dataGridCell.Column.DisplayIndex;
return new RowActionParameters
{
Item = dataGridCell.DataContext,
ColumnPropertyName = dataGridCell.Column.SafeAccess(x => x.SortMemberPath),
DataRowView = dataRowView,
ColumnIndex = columnIndex
};
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
你有沒有試過,我認爲當你觸發命令綁定執行。 – Bolu
@Bolu - 是的,我試過了,綁定在渲染數據網格時執行,而不是在單擊按鈕時執行。 – Giedrius
你想發送什麼命令參數,我想大部分可以通過按鈕的命令綁定完成 – Muds