Стоит такая задача: У меня есть ListView, где отображены данные из базы данных, вот код:
public ObservableCollection<Service> Services { get; set; }
private void Page_Loaded(object sender, RoutedEventArgs e)
{
Services = new ObservableCollection<Service>(DbContextObject.db.Service);
this.DataContext = this;
}
Когда я провожу удаление выбранной записи:
private void RemoveSelectedItem_Click(object sender, RoutedEventArgs e)
{
try
{
Service SelectedService = (Service)ListService.SelectedItem;
if (SelectedService != null)
if (MessageBox.Show("Вы действительно хотите удалить запись?", "Подтвердите удаление.", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
DbContextObject.db.Service.Remove(SelectedService);
DbContextObject.db.SaveChanges();
Page_Loaded(null, null);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
throw new Exception();
}
}
Запись удаляется, но данные на ListView не обновляются, вот код самой ListView:
<ListView Name="ListService"
ItemsSource="{Binding Services}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Image Source="{Binding MainImagePath}"
Width="150"
Height="150"/>
</Grid>
<Grid Grid.Column="1"
Margin="20,10">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="0.3*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0"
Margin="0,0,0,10">
<Label Content="{Binding Title}" ContentStringFormat="Наименование услуги: {0}" FontSize="18"/>
<Label Content="{Binding Cost}" ContentStringFormat="Стоимость: {0:C}" FontSize="14"/>
<Label Content="{Binding DurationInSeconds, Converter={StaticResource ResourceKey=Converter}}" FontSize="14"/>
<Label Content="{Binding Discount}" ContentStringFormat="* Скидка: {0}%" FontSize="14"/>
</StackPanel>
<StackPanel Grid.Row="1"
Orientation="Horizontal"
HorizontalAlignment="Left">
<Button Style="{StaticResource FunctionButtonStyle}"
Content="Редактировать"
Click="EditSelectedItem_Click"
Width="150">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="10"/>
</Style>
</Button.Resources>
</Button>
<Button Style="{StaticResource FunctionButtonStyle}"
Margin="20,0,0,0"
Content="Удалить"
Click="RemoveSelectedItem_Click"
Width="150">
<Button.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="10"/>
</Style>
</Button.Resources>
</Button>
</StackPanel>
</Grid>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Вопрос, как сделать так, чтобы после события удаления DataContext отобразил свежие данные из Базы Данных?