简体   繁体   中英

Delete Items in an ObservableCollection That are Bound to A GridView

I would like to be able to select an item that is bound to my gridview and press the delete button, which would delete the item from the observable collection to which the gridview is bound to.

<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.
    }
}

这不起作用:?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM