0

My WPF app have one MainWindow.xaml and it contains several DataGrid which is bind to ObservableCollection in MainWindow.xaml.cs.

For example,

public MainWindow()
{
    var dg1 = new ObservableCollection<CustomClass1>();
    var dg2 = new ObservableCollection<CustomClass2>();
    var dg3 = new ObservableCollection<CustomerClass3>();
}

Problem is MainWindow.xaml is getting longer and longer as I keep adding new DataGrid in it.

Can I separate each DataGrid to UserControl or Page? Or do you know better solution for handling several DataGrid in WPF app?

Thanks in advance!

jkl
  • 675
  • 2
  • 8
  • 23
  • read about virtualization of datagrids. maybe it's disabled in your case. https://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.enablerowvirtualization(v=vs.110).aspx – Mikolaytis Jul 06 '16 at 20:30

2 Answers2

1

Yes. It is possible. In my opinion, it is better to follow the MVVM approach in your case. Hope you are aware of it. If you are not aware of the approach please learn about it.

You can create 3 different user controls or if you feel one is enough, then one is just enough. Just create a user control and create a corresponding view model for that. In the viewmodel, you can bind the view’s datagrids’ source to properties, thereby reducing complexity, improving maintainability and readability of your code.

You will also not get the feeling that the MainWindow is getting complicated. Just let the MainWindow have few lines of code, in my opinion.

ViVi
  • 4,339
  • 8
  • 29
  • 52
  • But i read stackoverflow answer that a user control should not have its own view model? What do you think? Here is the link: http://stackoverflow.com/a/28815689/4546953 – jkl Jul 07 '16 at 12:08
  • 1
    Did you check the first answer which is also selected as the right answer?? This is what I think. It's upto you. **This is not a yes or no question. It depends on whether having extra view models affords you better maintainability or testability. There's no point adding view models if it doesn't gain you anything. You'll need to gauge whether the overhead is worth it to your particular use case.** – ViVi Jul 07 '16 at 12:11
  • Thanks! Since the second answer got most up-votes, i thought that was the most 'correct' answer. Thank you for your advice! – jkl Jul 07 '16 at 12:16
0

Yes, you can (and probably should) separate your DataGrids out into separate UserControls. Instead of keeping your collections in the code-behind, consider implementing the Model-View-ViewModel pattern (MVVM). Make sure to set the DataContext of each UserControl to an instance of the relevant View Model. This will prevent your current .xaml/.xaml.cs file from getting too large and will also help to make your components more re-usable.

Tim Oehler
  • 109
  • 3
  • But i read stackoverflow answer that a user control should not have its own view model? What do you think? Here is the link: http://stackoverflow.com/a/28815689/4546953 – jkl Jul 07 '16 at 12:09
  • If you choose to factor out your MainWindow.xaml in to multiple UserControls, that doesn't necessarily mean that you need multiple View Models as well. Indeed, you could have one ViewModel that acts as the DataContext to all 3 of your UserControls. My original comment was suggesting that you create a View Model in the MVVM sense. When you mentioned that your xaml.cs file was getting long, I assumed that meant you were doing your View-related logic in the code-behind, as opposed to a proper View Model. – Tim Oehler Jul 07 '16 at 15:21