using SPDemo.Commands; using SPDemo.Models; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace SPDemo.ViewModels { public class EmployeeViewModel : INotifyPropertyChanged { public ObservableCollection<Employee> _employeesList; public ObservableCollection<Employee> employeesList { get { return _employeesList; } set { _employeesList = value; OnPropertyChanged("employeesList"); } } public EmployeeViewModel() { Employee emp = new Employee(); employeesList = emp.GetAllEmployees(); DeleteCommand = new RelayCommand(DeleteSelected); } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string prop) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); } } public ICommand DeleteCommand { get; set; } private void DeleteSelected(object state) { if (null != state) { Employee emp = new Employee(); emp.RemoveEmployee(state as Employee); employeesList = emp.GetAllEmployees(); OnPropertyChanged("employeeList"); } } } }
<Window x:Class="SPDemo.Views.EmployeeView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:my="clr-namespace:SPDemo.ViewModels" Title="Employees" Height="300" Width="500"> <Window.Resources> <my:EmployeeViewModel x:Key="vmCommands"></my:EmployeeViewModel> </Window.Resources> <Window.DataContext> <my:EmployeeViewModel></my:EmployeeViewModel> </Window.DataContext> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <DataGrid ItemsSource="{Binding employeesList,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" SelectedValue="{Binding SelectedRow,Mode=TwoWay}" CanUserAddRows="False" CanUserDeleteRows="False"> <DataGrid.Columns> <DataGridTextColumn Header="Name" Binding="{Binding Name,Mode=TwoWay}"></DataGridTextColumn> <DataGridTextColumn Header="Address" Binding="{Binding Address,Mode=TwoWay}"></DataGridTextColumn> <DataGridTextColumn Header="Salary" Binding="{Binding Salary,Mode=TwoWay}"></DataGridTextColumn> <DataGridTextColumn Header="MobileNo" Binding="{Binding MobileNo,Mode=TwoWay}"></DataGridTextColumn> <DataGridTextColumn Header="Qualification" Binding="{Binding Qualification,Mode=TwoWay}"></DataGridTextColumn> <DataGridTemplateColumn Header="Actions"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Button Content="Delete" Foreground="Red" Margin="0,0,5,0" Command="{Binding Source={StaticResource vmCommands},Path=DeleteCommand}" CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"></Button> <Button Content="Edit" Foreground="Black"></Button> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> </Grid> </Window>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)