Events design - using events to trigger initialisation events - c#

I have a view class that listens for a change on any cell property in a list of cells.
New cells can be added at any time. What is the best way to tell my view class to listen to the new cells that have just been added without the cells knowing about the view class at all.
My current method is to use another event listener to listen to changes in the list. When this happens i add the listener to its relevent properites. Is that the best way to do this?
More info on my particular situation:
I have used events sparingly in other projects but this is the first time i am embracing them in a big way with one master data class and many different views that show the data in different formats.
My research on good design only extends as far as having read Head First design patterns so please do recommend any good resources on using the event pattern in a nice clean way as my project is beocming a difficult to track mess!
Thanks

Yup, what you describe is the common practice. Listen for changes to the list then in those handlers, hook into change events on the added items. The catch is to make sure you also remove those event handlers as items are dropped from the list - otherwise you will have memory leaks.

Related

How to display MainWindow before the business logic execution has completed

So I am new to MVVM with WPF and am having trouble with a few concepts.
So as soon as you run, my MainViewModel will set up the model and will start pulling from the database in order to populate a list which is binded to one of my controls.
Now the problem is that, after it pulls data from the db it then needs to listen to another source which will fire an event and then add to this list. Now, this data source will fire events infinitely, so I would like to display the UI with the DB-pulled data and then update this list as events fire.
My initial idea was to only start this listen for updates once I have loaded and presented my control, but how is this done using the OnLoaded event in MVVM, I think this has been my biggest struggle.
Thanks
Decided to go with Clemens' comment.
I now, bind the datasource in code and run the viewModel's listen method when the window is loaded, using the Loaded event. I am still not sure if this is a good MVVM solution so will probably change it again soon if it isn't.

The best practice to implement custom\user control in WPF

I have a list of custom controls that should look something like this
Before I start to implement them through a custom or user control in WPF (via MVVM), I want to ask if I do everything right. I create a DataTemplate and binding properties I need (these are the numeric values (0.13) in columns) and ItemTemplat'ing it to listview or listbox. Also I'm having an observable collection of viewmodels for these templates and every viewmodel sends some specific numeric data through short intervals from slave device. Also I need this green element to be clicked (just to add a button to a template I guess) and having displayed an additonal window with real time plots. So my question is: Is this the right approach I'm talking about or do I have something wrong? I'm quite new to WPF, so please excuse me. I dont think that it is a great challenge to implement something like this.
I'm rather new to this model as well, however one thing I have found that has helped me with managing multiple View Models has been an IOC Locator. An example can be found here:
http://dotnetpattern.com/mvvm-light-toolkit-example

What is the correct way to pass controls around?

I am creating a new winforms application that will have datagridviews which will load matrix data, and I want the user to be able to do a bunch of operations on the data, like showing/hiding columns, editing cell values, and filtering too.
I thought of using the MVP pattern (Model, View, Presenter).
I wanted to create a presenter class, which would handle all the logic (meaning any events that the user triggers), eventually end up in the presenter which will work on the raw data (the matrices). This seems logical up to now but my question is what do I do if I want to pass the controls themselves (like the datagridviews)? Should these controls be sent the presenter class or is that bad design?
Perhaps it's better to find ways to only modify the raw data and then update my datagridviews?
It is not a good idea to pass around controls. If you're going to use a pattern such as "MVP", then you should have the "model" contain the representation of the "view". In this case if there are various details pertaining to a set of data it belongs in the model. Then you pass the model around.
Perhaps it's better to find ways to only modify the raw data and then update my datagridviews?
So, to answer this question, "yes". Use the model to wrap the data and pass it around.
Update
Specifically, with WinForms controls belong to containers, being that they are reference types and have lots of exposed events you run a HUGE risk of passing a reference from one Form to another Form. Now, imagine that the first Form is done being used and closes and disposes, it kills the reference to the control and attempts to unwire events. Do you see where I'm going with this? It quickly becomes a nightmare trying to correctly cleanup references, and un wire event handler and since controls belong to one container by design it breaks that paradigm.
It's better to have a separation of concerns. If you need a view to have certain data, it's always best to pass around the data itself...

Passing data from a handler to another. very basic issue about OOP

This may be a really basic question to experienced programmers but I started on VB6, and now I'm trying to accomplish same stuff on C# which is object oriented.
Suppose I have a class with a method to add two numbers on textboxes and I run that in the click handler of a button (Doesn't matter if it is static or not), then I have the result and I display it on the screen (maybe in another textbox), the user click another button, how do I recover the result on the other button handler?, what's the best practice?, I know I can read the result on the textbox, but if the result was displayed on a Messagebox.Show or in console?.
What's the best practice to save results instead of using helper textboxes or global variables?
On VB6 I use invisible textboxes, so my forms looks really messy, but thats the way to there.
Using C# + XAML + WPF
Usually that is what a model is for. It is the data-state of what is shown (and more, as some information may not be displayed at all times or just used as utility). The view often has a reference to the model which you then can access in the handlers and manipulate.
I'd suggest reading up on design patterns like Model-View-Controller and for WPF specifially Model-View-ViewModel.
Also, WPF has a few powerful mechanics like data binding, which makes synchronizing your data with the view a lot easier, do not treat your view as a model.

C# catch control's change event from its container

In my project I have a settings form. Where if any changes happen I have to notify user about it if he wants to leave that page without saving his changes. At this time I am doing this by catching every control change event. I am sure there is a better way - like catching the change event from its container. Is it possible?
Rather than worrying about the controls directly, how about creating a Settings class that implements interfaces from System.ComponentModel like INotifyPropertyChanged and IDataErrorInfo and use data binding to get the values in and out of the controls.
Your Settings class can then not only record whether anything has changed but also make validation of the user input easier.
A good place to start is MSDN.
You have the right solution, but you may want to be very generic about catching the change events. For example, you could try something like this right after the InitializeComponent(); line in the constructor:
foreach(Control c in Controls) {
c.TextChanged += new EventHandler(genericTextBox_TextChanged);
}
genericTextBox_TextChanged would set a form-wide hasChanged flag to true, or something really basic like that. You may need to make this into a recursive function that loops through all of the children of c if it has child controls.
Let me offer you some kind of a workaround. My offer is to create a custom DataSet. Then add tables corresponding to the form controls. After this you can bind each form control to this dataset.Pros: You keep all the controls data-bound. So you don't need to care about the changing of particular control. You have just to control dataset changes. Cons (maybe): after this you should rewrite settings preview mechanism. Instead of changing controls you have to change data. IMO, it's not so hard, but I have no idea about this approach in your applicationI think this approach will be, at least, easy to debug.
If it's web, look at the unload event for javascript

Categories

Resources