[英]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.