使用命令+;删除listview中的行;WPF+中的CommandParameter;MVVM
我想使用如下命令和CommandParameter删除ListView控件中的一行使用命令+;删除listview中的行;WPF+中的CommandParameter;MVVM,wpf,listview,command,commandparameter,Wpf,Listview,Command,Commandparameter,我想使用如下命令和CommandParameter删除ListView控件中的一行 <GridViewColumn Header="X"> <GridViewColumn.CellTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding CriteriaId}"/> <Button Name="
<GridViewColumn Header="X">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding CriteriaId}"/>
<Button Name="btnDeleteCriterion" Tag="{Binding CriteriaId}" Content="{Binding CriteriaId}" Foreground="Red" FontWeight="Bold"
Command="{Binding DeleteCriterionCommand}"
DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}"
CommandParameter="{Binding RelativeSource={RelativeSource self}, Path=Tag}"
/>
<StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
但我总是将criterionId参数设置为null
我做错了什么?只要你的按钮的
标记不是空的(如果是空的,你就有不同的问题),我看不出有什么原因你不能像对待标记那样直接绑定到CriterionId
,因为你在按钮中显式地设置了DataContext,当您执行像{binding SomeProperty}
这样的绑定时,它将假定SomeProperty位于您刚才设置的DataContext中。尝试使用更显式的绑定,如:
"{Binding RelativeSource={RelativeSource AncestorType=StackPanel}, Path=DataContext.CriteriaId}"
这将为您提供正确的DataContext的CriteriaID
,就像它用于TextBlock
嗨,Jakub,我在上面添加了一个TextBlock,它的绑定与Button的标记相同,但是TextBlock得到更新,而不是标记或按钮的内容!哦,我想这一定与按钮的DataContext有关!是的,有效:)。但只需稍加更正
“{Binding RelativeSource={RelativeSource AncestorType=StackPanel},Path=DataContext.CriteriaId}”
。谢谢大家,我添加了StackPanel来容纳TextBlock。现在我不需要TextBlock,也不需要StackPanel。在这种情况下,如果我在您建议的答案中设置Path=“GridViewColumn”,它将不起作用。我必须保留StackPanel才能工作吗?我相信你应该能够绑定到DataTemplate
我尝试过这个,CommandParameter=“{Binding RelativeSource={RelativeSource AncestorType=DataTemplate},Path=DataContext.CriteriaId}”
但它也不工作。噢!我没有意识到DataTemplate
没有DataContext
属性。尝试以下操作:{Binding Path=CriteriaId,RelativeSource={RelativeSource TemplatedParent}
。如果这不起作用,就作弊并在堆叠框架内使用按钮。
"{Binding RelativeSource={RelativeSource AncestorType=StackPanel}, Path=DataContext.CriteriaId}"