[英]DataGrid CellTemplate, Pass DataContext as Command Parameter
我有一个DataGrid
,其中ItemsSource
设置为ObservableCollection<ObservablePuls>
。
在最后一栏中,我有一个按钮,我想将其CommandParameter
绑定到ObservablePuls
。 有了这段代码,我将null传递为CommandParameter
和此错误:
BindingExpression路径错误:在'对象''ObservablePuls'(HashCode = 40133923)'上找不到'DataContext'属性。 BindingExpression:Path = DataContext; DataItem ='ObservablePuls'(HashCode = 40133923); 目标元素是'Button'(Name =''); 目标属性是“ CommandParameter”(类型“ Object”)
所以这是我的问题: 如何传递作为行源的对象?
这是带按钮的DataGrid
代码(其他列省略):
<DataGrid x:Name="PulsesDataGrid"
Grid.Row="1"
ItemsSource="{Binding ObservableUser.Pulses}"
CanUserAddRows="False"
CanUserSortColumns="True"
AutoGenerateColumns="False"
materialDesign:DataGridAssist.CellPadding="13 8 8 8"
materialDesign:DataGridAssist.ColumnHeaderPadding="8"
CanUserDeleteRows="False"
CanUserReorderColumns="True"
HorizontalAlignment="Stretch">
<DataGrid.Columns>
//... Other columns here
<DataGridTemplateColumn Header="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Style="{StaticResource MaterialDesignToolButton}"
Margin="10 0 0 0"
Command="{Binding ElementName=PulsesDataGrid, Path=DataContext.DeleteRowInDataGrid}"
CommandParameter="{Binding Path=DataContext}">
<materialDesign:PackIcon Kind="DeleteForever" />
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
绑定Command
很好,因为它是用null参数来调用的;(
您可以使用以下方法将DataTemplate(当前行中显示的元素)中的当前DataContext绑定到CommandParameter:
CommandParameter="{Binding}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.