Monday, September 30, 2013

WPF-DataGrid and SubDataGrid using MVVM

Dear All,



I have written some logic using MVVM pattern and tried to show a sub-datagrid in each row of a datagrid however there are some issues that I am currently facing and they are as follows (screenshot attached) :-In each row I have an expander in which I want to control the RowDetailsVisibilityMode of my sub datagrid.




For selected checkboxes the rows should be deleted when clicked on Delete button at the end.



Upon clicking of the Edit button, one can edit a particular row and again the rows become readonly.



Please suggest some approach on how to do this and kindly find my code as follows :-



View :-



ViewModel :-



using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.ComponentModel; using System.Collections.ObjectModel; using System.Windows.Input; using System.Windows.Data; using Microsoft.Practices.Prism.Commands; namespace WIMOProjectDemo.ViewModel { public class CustomerManagementModuleVM : INotifyPropertyChanged { #region Fields private ICollectionView lstsource; private string filterText; private DelegateCommand delegateCommandDelete; private DelegateCommand delegateCommandEdit; #endregion #region Public Variables public ObservableCollection sublst; #endregion #region Properties public ICollectionView lstsource { get { return lstsource; } set { lstsource = value; OnPropertyChanged("lstsource"); } } public string FilterText { get { return filterText; } set { filterText = value; OnPropertyChanged("FilterText"); FilterCollection(); } } #endregion #region Commands public DelegateCommand DelegateCommandDelete { get { return this.delegateCommandDelete ?? new DelegateCommand(FireMethod); } } public DelegateCommand DelegateCommandEdit { get { return this.delegateCommandEdit ?? new DelegateCommand(EditRowOnEditButton); } } #endregion #region Constructors public CustomerManagementModuleVM() { FillDataInGrid(); lstsource = CollectionViewSource.GetDefaultView(ParentGridData); lstsource.Filter = new Predicate(Filter); } #endregion #region Public Methods public bool Filter(object obj) { var data = obj as ParentGridClass; if (data != null) { if (!string.IsNullOrEmpty(filterText)) { return data.Customer.Equals(filterText) || data.Name.Equals(filterText) || data.Access.Equals(filterText) || data.Email.Equals(filterText) || data.Phone.Equals(filterText) || data.Fax.Equals(filterText) || data.Country.Equals(filterText) || data.Payment.Equals(filterText); } return true; } return false; } public IEnumerable ParentGridData { get { yield return new ParentGridClass { Customer = 324234, Name = "sgsfdd", Access = "Private", Email = "sdf@sfd.com", Phone = 56565, Fax = 45454, First = DateTime.Now.ToShortDateString(), Last = DateTime.Now.ToShortDateString(), Country = "India", Payment = "Card", SubGridData = sublst }; yield return new ParentGridClass { Customer = 65464, Name = "bdcgdf", Access = "Public", Email = "dfgfg@sfd.com", Phone = 54646, Fax = 45443, First = DateTime.Now.ToShortDateString(), Last = DateTime.Now.ToShortDateString(), Country = "India", Payment = "Cheque", SubGridData = sublst }; yield return new ParentGridClass { Customer = 53432, Name = "dsaf", Access = "Public", Email = "hg@sfd.com", Phone = 23423, Fax = 86786, First = DateTime.Now.ToShortDateString(), Last = DateTime.Now.ToShortDateString(), Country = "India", Payment = "Cash", SubGridData = sublst }; yield return new ParentGridClass { Customer = 68767, Name = "ghgf", Access = "Private", Email = "sdf@sfd.com", Phone = 56785, Fax = 85464, First = DateTime.Now.ToShortDateString(), Last = DateTime.Now.ToShortDateString(), Country = "India", Payment = "Cheque", SubGridData = sublst }; yield return new ParentGridClass { Customer = 345, Name = "ytryh", Access = "Private", Email = "fgdg@sfd.com", Phone = 54546, Fax = 89878, First = DateTime.Now.ToShortDateString(), Last = DateTime.Now.ToShortDateString(), Country = "Germany", Payment = "Card", SubGridData = sublst }; yield return new ParentGridClass { Customer = 655, Name = "ret", Access = "Private", Email = "cvbcvb@sfd.com", Phone = 98797, Fax = 32433, First = DateTime.Now.ToShortDateString(), Last = DateTime.Now.ToShortDateString(), Country = "Germany", Payment = "Card", SubGridData = sublst }; yield return new ParentGridClass { Customer = 234234, Name = "fghfgh", Access = "Public", Email = "dgdfsd@sfd.com", Phone = 67886, Fax = 56555, First = DateTime.Now.ToShortDateString(), Last = DateTime.Now.ToShortDateString(), Country = "Germany", Payment = "Cheque", SubGridData = sublst }; yield return new ParentGridClass { Customer = 678678, Name = "cvxcv", Access = "Public", Email = "fdgfg@sfd.com", Phone = 56567, Fax = 45777, First = DateTime.Now.ToShortDateString(), Last = DateTime.Now.ToShortDateString(), Country = "Germany", Payment = "Card", SubGridData = sublst }; yield return new ParentGridClass { Customer = 97887, Name = "vbnvn", Access = "Public", Email = "dfgh@sfd.com", Phone = 87990, Fax = 43555, First = DateTime.Now.ToShortDateString(), Last = DateTime.Now.ToShortDateString(), Country = "US", Payment = "Cash", SubGridData = sublst }; yield return new ParentGridClass { Customer = 546, Name = "adsasd", Access = "Public", Email = "dfg@sfd.com", Phone = 87900, Fax = 43533, First = DateTime.Now.ToShortDateString(), Last = DateTime.Now.ToShortDateString(), Country = "US", Payment = "Card", SubGridData = sublst }; yield return new ParentGridClass { Customer = 54677, Name = "uyiui", Access = "Public", Email = "sdfds@sfd.com", Phone = 65756, Fax = 56666, First = DateTime.Now.ToShortDateString(), Last = DateTime.Now.ToShortDateString(), Country = "US", Payment = "Card", SubGridData = sublst }; yield return new ParentGridClass { Customer = 8977, Name = "qweqwe", Access = "Private", Email = "tyry@sfd.com", Phone = 98089, Fax = 34577, First = DateTime.Now.ToShortDateString(), Last = DateTime.Now.ToShortDateString(), Country = "US", Payment = "Cash", SubGridData = sublst }; } } #endregion #region Private Methods private void FillDataInGrid() { sublst = new ObservableCollection(); sublst.Add(new ChildGridClass() { Name = "dsf", Email = "asd@dsa.com", Phone = 4566555 }); sublst.Add(new ChildGridClass() { Name = "rty", Email = "fhg@dsa.com", Phone = 3243433 }); sublst.Add(new ChildGridClass() { Name = "hjk", Email = "cvx@dsa.com", Phone = 9788887 }); sublst.Add(new ChildGridClass() { Name = "qwe", Email = "ret@dsa.com", Phone = 2254884 }); } private void FireMethod(DataGrid e) { ObservableCollection lstsourceNew = lstsource as ObservableCollection; for (int i = 0; i < e.Items.Count - 1; i++) { CheckBox x = e.Columns[0].GetCellContent(e.Items[i]) as CheckBox; if (x.IsChecked == true) { MessageBox.Show(e.Items.Count.ToString()); lstsourceNew.RemoveAt(i); lstsource = lstsourceNew as ICollectionView; OnPropertyChanged("lstsource"); } } MessageBox.Show("Deleted"); } private void FilterCollection() { if (lstsource != null) { lstsource.Refresh(); } } private void EditRowOnEditButton(DataGrid e) { OnPropertyChanged("lstsource"); MessageBox.Show("Edited"); } #endregion #region OnPropertyChangedEvent public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } #endregion } #region Public Classes public class ParentGridClass { public int Customer { get; set; } public string Name { get; set; } public string Access { get; set; } public string Email { get; set; } public int Phone { get; set; } public int Fax { get; set; } public string First { get; set; } public string Last { get; set; } public string Country { get; set; } public string Payment { get; set; } public ObservableCollection SubGridData { get; set; } } public class ChildGridClass { public string Name { get; set; } public string Email { get; set; } public int Phone { get; set; } } #endregion }



xaml.cs :-



using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WIMOProjectDemo.View { /// /// Interaction logic for CustomerManagementModule.xaml /// public partial class CustomerManagementModule : UserControl { public CustomerManagementModule() { InitializeComponent(); } private void RowDoubleClick(object sender, RoutedEventArgs e) { var row = (DataGridRow)sender; row.DetailsVisibility = row.DetailsVisibility == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed; } } }
Full Post

No comments:

Post a Comment