private void DeleteTheme_Click(object sender, RoutedEventArgs e) { var dg = sender as DataGrid; for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual) { if (vis is DataGridRow) { if (((System.Windows.FrameworkElement)vis).DataContext is DTOTheme) { var themeId = ((DTO.DTOTheme)((System.Windows.FrameworkElement)vis).DataContext).Id; if (themeId != -1) { client.DeleteTheme(themeId); this.SetListOfThemes(); } } } } }