none
How to delete selected row from a datagridwith context menu? RRS feed

  • Question

  • I need to be able to delete a selected row from a datagrid with context menu item. I have hard time finding the path to the selected row. I am wondering if someone can look at the code below and let me know what will be the appropriate to do within my code. Thank you in advance.

    XAML:

    <UserControl.DataContext>   
    		<local:MainPage_ViewModel/> 
    	</UserControl.DataContext> 
    	<StackPanel Orientation="Horizontal"> 
    		
    		<data:DataGrid ItemsSource="{Binding Coordinates}" AutoGenerateColumns="False" Margin="10">
          <!-- ContextMenu -->
    			 <toolkit:ContextMenuService.ContextMenu>
    				 <toolkit:ContextMenu x:Name="Menu" >
              <toolkit:MenuItem x:Name="Edit" />
              <toolkit:MenuItem x:Name="Delete" Tag="{Binding}" Click="Button_Click" />
            </toolkit:ContextMenu>
          </toolkit:ContextMenuService.ContextMenu>   
    			<data:DataGrid.Columns>       
    				<data:DataGridTextColumn Header="X Position" Width="100" Binding="{Binding X}"/>       
    				<data:DataGridTextColumn Header="Y Position" Width="100" Binding="{Binding Y}"/>       
    				<data:DataGridTemplateColumn Header="Delete Item" Width="100">         
    					<data:DataGridTemplateColumn.CellTemplate>           
    						<DataTemplate>             
    							<Button Content="Delete" Tag="{Binding}" Click="Button_Click"/>           
    						</DataTemplate>         
    					</data:DataGridTemplateColumn.CellTemplate>       
    				</data:DataGridTemplateColumn>     
    			</data:DataGrid.Columns>   
    		</data:DataGrid>
    

    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 + ")"; }   
    		} 
    	}
    
    Sunday, March 20, 2011 11:54 PM

Answers

All replies