I am wondering if someone will be so kind to show how it is possible. I need to be able to delete selected row in DataGrid with MenuItem from ContextMenu? I generate content withobservableCollection using MVVM patter. Below is the code. Any help is highly appreciated! Thank you!
Code behind:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.viewModel = this.DataContext as MainPage_ViewModel;
} private MainPage_ViewModel viewModel;
private void Button_Click(object sender, RoutedEventArgs e)
{
viewModel.DeleteCoordinate((sender as Button).Tag as Coordinate_DataViewModel);
}
}
ViewModel Code:
public class MainPage_ViewModel : INotifyPropertyChanged
{
public MainPage_ViewModel()
{
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 1, Y = 2 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 2, Y = 4 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 3, Y = 6 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 4, Y = 8 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 5, Y = 10 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 6, Y = 12 }));
}
public ObservableCollection<Coordinate_DataViewModel> Coordinates
{
get { return coordinates; }
set
{ if (coordinates != value)
{
coordinates = value;
OnPropertyChanged("Coordinates");
}
}
}
private ObservableCollection<Coordinate_DataViewModel> coordinates = new ObservableCollection<Coordinate_DataViewModel>();
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public void DeleteCoordinate(Coordinate_DataViewModel dvmToDelete)
{
coordinates.Remove(dvmToDelete);
}
public void UpdateCoordinate(Coordinate_DataViewModel dvmToDelete)
{
}
}
//Model
public class Coordinate_Model
{
public double X;
public double Y;
}
//DataViewModel
public class Coordinate_DataViewModel
{
public Coordinate_DataViewModel(Coordinate_Model model)
{
this.underlyingModel = model;
}
private Coordinate_Model underlyingModel;
public double X
{
get { return underlyingModel.X; }
set { underlyingModel.X = value; }
}
public double Y
{
get { return underlyingModel.Y; }
set { underlyingModel.Y = value; }
} public string XYCoordinate
{
get { return "(" + X + "," + Y + ")"; }
}
}
Code behind:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.viewModel = this.DataContext as MainPage_ViewModel;
} private MainPage_ViewModel viewModel;
private void Button_Click(object sender, RoutedEventArgs e)
{
viewModel.DeleteCoordinate((sender as Button).Tag as Coordinate_DataViewModel);
}
}
ViewModel Code:
public class MainPage_ViewModel : INotifyPropertyChanged
{
public MainPage_ViewModel()
{
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 1, Y = 2 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 2, Y = 4 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 3, Y = 6 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 4, Y = 8 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 5, Y = 10 }));
coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 6, Y = 12 }));
}
public ObservableCollection<Coordinate_DataViewModel> Coordinates
{
get { return coordinates; }
set
{ if (coordinates != value)
{
coordinates = value;
OnPropertyChanged("Coordinates");
}
}
}
private ObservableCollection<Coordinate_DataViewModel> coordinates = new ObservableCollection<Coordinate_DataViewModel>();
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public void DeleteCoordinate(Coordinate_DataViewModel dvmToDelete)
{
coordinates.Remove(dvmToDelete);
}
public void UpdateCoordinate(Coordinate_DataViewModel dvmToDelete)
{
}
}
//Model
public class Coordinate_Model
{
public double X;
public double Y;
}
//DataViewModel
public class Coordinate_DataViewModel
{
public Coordinate_DataViewModel(Coordinate_Model model)
{
this.underlyingModel = model;
}
private Coordinate_Model underlyingModel;
public double X
{
get { return underlyingModel.X; }
set { underlyingModel.X = value; }
}
public double Y
{
get { return underlyingModel.Y; }
set { underlyingModel.Y = value; }
} public string XYCoordinate
{
get { return "(" + X + "," + Y + ")"; }
}
}