Chào anh Long, anh cho em hỏi: Em có 1 vấn đề là em có 1 bảng DIENVIEN trong CSDL, giờ em xóa trên giao diện rồi mà làm sao em xóa dưới CSDL. Mong anh và mọi người giúp đỡ em
MainWindow.xaml
<Window x:Class="Test_MVVM.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Test_MVVM"
xmlns:Duc="clr-namespace:ViewModel;assembly=ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<Duc:DIENVIENVIEWMODEL></Duc:DIENVIENVIEWMODEL>
</Window.DataContext>
<StackPanel>
<ListView x:Name="lv1" ItemsSource="{Binding Listdienvien}">
<ListView.View>
<GridView>
<GridViewColumn Header="Mã diễn viên">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ID}"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Tên diễn viên">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TEN}"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<TextBlock></TextBlock>
<StackPanel>
<TextBlock Text="Mã diễn viên"></TextBlock>
<TextBox Text="{Binding SelectedValue.ID, ElementName=lv1,Mode=OneWay}"></TextBox>
<TextBlock Text="Tên diễn viên"></TextBlock>
<TextBox Text="{Binding SelectedValue.TEN, ElementName=lv1,Mode=OneWay}"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Content="Xóa" Command="{Binding DeleteCommand}" CommandParameter="{Binding SelectedValue,ElementName=lv1}"></Button>
</StackPanel>
</StackPanel>
</Window>
DataProvider.cs (trong Project Model)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
public class DataProvider
{
private static DataProvider instance;
public static DataProvider Instance
{
get { if (instance == null) instance = new DataProvider(); return DataProvider.instance; }
private set { DataProvider.instance = value; }
}
public DataClasses1DataContext Data { get; set; }
private DataProvider()
{
Data = new DataClasses1DataContext();
}
}
}
DIENVIENVIEWMODEL.cs (trong project ViewModel)
using Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace ViewModel
{
public class DIENVIENVIEWMODEL
{
ObservableCollection<DIENVVIEN> listdienvien;
public ObservableCollection<DIENVVIEN> Listdienvien { get => listdienvien; set => listdienvien = value; }
public DIENVIENVIEWMODEL()
{
Listdienvien = new ObservableCollection<DIENVVIEN>(DataProvider.Instance.Data.DIENVVIENs);
DeleteCommand = new RelayCommand<object>((p) => p != null, (p) =>
{
Listdienvien.Remove(p as DIENVVIEN);
});
}
public ICommand DeleteCommand { get; set; }
}
}
Bạn thử đoạn này nhé