删除绑定到GridView的ObservableCollection中的项目

时间:2011-09-20 01:27:52

标签: c# .net wpf data-binding gridview

我希望能够选择绑定到gridview的项目并按下删除按钮,这将删除gridview绑定到的可观察集合中的项目。

<ListView Height="166" HorizontalAlignment="Left" Margin="12,290,0,0" Name="listView1" VerticalAlignment="Top" Width="636" ItemsSource="{Binding SearchTerms}" KeyUp="SearchTermsGrid_KeyPressed">
    <ListView.View>
        <GridView x:Name="SearchTermsGrid">
            <GridViewColumn Width="155" Header="Search Term" DisplayMemberBinding="{Binding Name}"/>
            <GridViewColumn Width="90" Header="Status" DisplayMemberBinding="{Binding Status}"/>
            <GridViewColumn Width="60" Header="Count" DisplayMemberBinding="{Binding NumberToDownload}"/>
            <GridViewColumn Width="60" Header="Size" DisplayMemberBinding="{Binding Size}"/>
            <GridViewColumn Width="60" Header="Type" DisplayMemberBinding="{Binding Type}"/>
        </GridView>
    </ListView.View>
</ListView>
ObservableCollection<SearchTerm> _searchTerms = new ObservableCollection<SearchTerm>();
public ObservableCollection<SearchTerm> SearchTerms
{
    get
    {
        return _searchTerms;
    }
}

private void SearchTermsGrid_KeyPressed(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Delete)
    {
        //I dunno, this might be a bad approach.
    }
}

1 个答案:

答案 0 :(得分:2)

这不起作用:?

this._searchTerms.Remove(this.listView1.SelectedItem);