列表视图 mvvm 中的 WPF 删除按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13584643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Delete button in listview mvvm
提问by Chamika Sandamal
I have a button inside a listview to delete selected item.when i click on the button RemoveSubjectCommand is not firing. if i put the button outside the listview it is working fine. hopw this is just because of nested item. how can i solve this problem?
我在列表视图中有一个按钮来删除所选项目。当我点击按钮 RemoveSubjectCommand 没有触发时。如果我把按钮放在列表视图之外,它工作正常。hopw 这只是因为嵌套项目。我怎么解决这个问题?
<ListView HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
HorizontalContentAlignment="Stretch" Grid.ColumnSpan="3" Grid.Row="2"
ItemsSource="{Binding AssignedSubjects}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Subjects" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="X" Command="{Binding RemoveSubjectCommand}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
View Model,
查看模型,
private ICommand removeSubjectCommand;
**
public ICommand RemoveSubjectCommand
{
get { return removeSubjectCommand ?? (removeSubjectCommand = new RelayCommand(param => this.RemoveSubject(), null)); }
}
**
private void RemoveSubject()
{ ***
}
If i put following code, it will work fine.
如果我输入以下代码,它将正常工作。
<ListView.InputBindings>
<KeyBinding Key="Delete" Command="{Binding RemoveSubjectCommand}" />
</ListView.InputBindings>
回答by Arsen Mkrtchyan
That is because DataContext of button is ListBoxItem DataContext. So you need to go to parent ListView DataContext.
这是因为按钮的DataContext 是ListBoxItem DataContext。所以你需要去父ListView DataContext。
one way to do that, is to give ListView a name, and to bind with element name
一种方法是给 ListView 一个名称,并与元素名称绑定
<ListView Name="lv" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
HorizontalContentAlignment="Stretch" Grid.ColumnSpan="3" Grid.Row="2"
ItemsSource="{Binding AssignedSubjects}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Subjects" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="X" Command="{Binding ElementName=lv,Path=DataContext.RemoveSubjectCommand}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
回答by Blachshma
You need to bind the Command to using FindAncestor. Otherwise, it'll try to bind to a RemoveSubjectCommandusing the ListViewItem's datacontext, not the ViewModel's.
您需要将 Command 绑定到使用FindAncestor。否则,它会尝试RemoveSubjectCommand使用ListViewItem的数据上下文绑定到 a ,而不是 ViewModel 的。
Try this:
尝试这个:
<Button Content="X" Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}, Path=DataContext.RemoveSubjectCommand}" />
回答by Dennis
if i put the button outside the listview it is working fine
如果我把按钮放在列表视图之外,它工作正常
Because RemoveSubjectCommandproperty is defined in the data context of ListView, not in the data context of item.
因为RemoveSubjectCommandproperty 是在 的数据上下文中定义的ListView,而不是在item的数据上下文中定义的。

