Proper application design: reading data and UserControls

This may be a long question, I will do my best to ask him correctly.

I want to read large .txt files with lots of numeric data. In each file there will be โ€œchannelsโ€ (30-50 channels, with its own name, axis units and, of course, data). So I created a class Channel with these properties and a class File that has a list of these channels. It also has a method that reads a file and saves everything in lists.

I want to be able to upload multiple files at the same time, and for this I created a UserControl , which consists of a button for loading a file and a ComboBox that displays each channel:

enter image description here ( ComboBox bound to ObservableCollection ) Data is stored in code behind the user control. Therefore, when I insert several UserControls in MainWindow , I cannot access this data.

I want to have multiple UserControl ...

enter image description here

... and be able to display data from each in this Plotter and be capable of some control (previous, next ..)

enter image description here

My first approach was to store everything in a UserControl , which was easy to do but inefficient. So, I realized that the solution could be to store everything in a different place, and then access it from MainWindow or another place. I tried VERY GEST to learn MVVM and use it in my project. But I have to be very STUPID, because I canโ€™t get it.

My new approach is described in the following diagram:

enter image description here

My questions:

1. Is this the right way to do this? If MVVM is a way, please, please explain to me a bit at the beginning, because I am not able to translate these complex MVVM examples into my project.

2. If I do this more or less correctly, how can I store all this data in a different place and access it from MainWindow ? (in my File class, I have a method that stores everything in lists, so in my UserControl I have a Browse button that gets the file name, and then using the read () method I store everything inside (?) File class , or at least inside the place where I created the new file: UserControl ).

I will send a code, photo, additional information, anything if necessary. Thanks.

+4
source share
1 answer

If MVVM is a way, please, please, please give me a little first, because I am not able to translate these complex MVVM examples into my project.

You are already halfway to using something that MVVM, at least in nature. This is not a "way", but it will definitely be a (reasonably good) way to handle it.

To design this using the MVVM type, you want your "MY DATA" class to be a DataContext UserControl. All data will be stored there (preferably in an ObservableCollection<T> instead of List<T> , as this will bind the binding more efficiently).

Your "UserControl1" part is likely to be some form of ItemsControl related to a set of sources. The selected item can be tied to something in your "MY DATA" class, which determines which "chart" should be displayed.

To answer your questions directly:

1. Is this the right way to do this?

This is definitely a step in the right direction. Keeping your data separate from your controls is one of the key elements to make your application more flexible and maintainable (and the big goal of MVVM).

2. If I do this more or less correctly, how can I store all this data in a different place and access it from MainWindow?

You do this by setting the class as the DataContext your UserControl and / or MainWindow . This allows you to bind to the properties of your data class (which is effectively a ViewModel in MVVM terminology).


In addition, I know that you tried to learn and learn MVVM - and this is hard to understand initially, but it is worth the effort. I will say that your design scenario (which is actually a list of โ€œoptionsโ€ on the left and the โ€œdetailโ€ panel on the right) is not unusual - it really looks like my example in my MVVM blog series , and it will need to be easy to create as soon as you will understand the basics.

+2
source

Source: https://habr.com/ru/post/1484230/


All Articles