Reputation: 1118
I have a ObservableCollection which holds records of custom type Item. I use that collection as a binding for a ListView in WPF. That class Item also implements the interface INotifyPropertyChanged. When I update some of the records in the ListView, I can see the change in the ListView.
All this data comes from a WCF service. When I call the client object for the service and call a delete method to delete an item that belongs to the ObservableCollection, the record gets deleted in the DB but the items in the ListView are not updated. Is this how it is supposed to work, or should I remove the item from the collection manually when I delete it from the DB with the service call?
Thanks
Here is the XAML:
<ListView ItemsSource="{Binding AllItems}" Height="244" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Left" Margin="1,25,0,0" Name="listView1" VerticalAlignment="Top" Width="485" >
<ListView.ContextMenu>
<ContextMenu AllowDrop="False">
<MenuItem Name="openRecord" Header="Open" Click="ContextMenuItem_Click" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" />
<MenuItem Name="deleteRecord" Header="Delete" Click="ContextMenuItem_Click" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}" />
</ContextMenu>
</ListView.ContextMenu>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<EventSetter Event="MouseDoubleClick" Handler="list_UserItems_ItemMouseDoubleClick" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Title}" Header="Title" Width="200" />
<GridViewColumn DisplayMemberBinding="{Binding CreatedByName}" Header="Created By" Width="100" />
<GridViewColumn DisplayMemberBinding="{Binding CreatedOn}" Header="Created On" Width="100" />
<GridViewColumn DisplayMemberBinding="{Binding ExpirationDate}" Header="Expires" Width="100" />
</GridView>
</ListView.View>
</ListView>
Upvotes: 1
Views: 855
Reputation: 30097
If I understand your situation you have two things in place
1 - An ObservableCollection
for showing data in ListView
which you are getting through WCF
Service.
2 - You are performing delete operation through WCF on DB
Most probably you will be calling WCF
service method to delete record from DB and passing it object which have to be deleted in parameters. If this or similar is the case then you are not making any change in ObservableCollection
which is bound to ListView
. This means your View won't be affected in any way
You should delete the record from ObservableCollection
manually to update the list or after deletion you should ask for new list from DB
through WCF
and replace the old ObservableCollection
with new list retrieved through WCF
Upvotes: 4