Do we really need BindingSource to bind to controls? I.e. what problems does it solve?
Do you use the BindingSource, or do you use an alternative method?
Yes, you need it regardless of your underlying data source. The primary raison-d'ĂȘtre of BindingSource is to avoid memory leaks that can occur when subscribing directly to change notification events on data objects. As long as you're using the default Windows Forms binding mechanism, you'll need BindingSource.
Related
I am working on a WPF application using MVVM pattern. I need to do data validation(using Data Annotations) for my data entry screens.
But the tricky part to write a common piece of code is rather than using simple property I need to use properties with are calling GetValue and SetValue method. But my entities are being generated by entity framework using templates and modifying template to achieve this things seems very difficult.
I am using this technique to validate my data
http://blogs.microsoft.co.il/blogs/tomershamam/archive/2010/10/28/wpf-data-validation-using-net-data-annotations-part-ii.aspx
http://blogs.microsoft.co.il/blogs/tomershamam/archive/2010/10/11/notifyingobject-for-wpf-amp-silverlight.aspx
If there is better and authentic way to validate the entity data, i'll be more than happy to change my approach or if I need to change the template then please point me to some great reference.
Firstly. Really bad idea to do this, not because it is hard, but due to that you need to inherit DependencyObject. DependencyObject is an STA Threaded class. You won't be able to use a worker thread to instantiate your objects, which means you can't use a worker thread to use Entity Framework.
Now, I suggest you use the MVVM pattern with WPF. You should have a View bind to a ViewModel, which then binds to your Entity Framework POCO objects.
You can try IValidateObject for you validation https://stackoverflow.com/questions/13917890/mvc4-custom-validation-via-ivalidateobject-validate-method-not-working
I personally however prefer the FluentValidation library.
I'm looking for a bit of advice if possible. I am using event handlers to update a WPF application, it works fine but I was wondering if this was the best way to go? Would it be better practice to use data binding using XAML?
Thanks for your help as always.
Personally I would use data binding to do this and the MVVM pattern.
At the most basic you would bind your textbox to a property on your view model, in the setter you could call a method which executes your SQL query. Your View Model class would also need to implement INotifyPropertyChanged to allow the view model to update the UI if you wanted data to flow both ways.
Although depending on what you're doing you may be better having something that triggers the SQL update rather than every time the text changes. If you have a lot of different fields to update and you use the text changed event you're going to fire off a lot more SQL updates than you need. Maybe a save button or something which detects when changes are finished and makes them all at once would be more efficient?
Sometimes a Domain Model object with a business logic (DDD) when calling a method an event is fired.
In my situation, a viewmodel (for a given view) encapsulates the domain object and needs to register and react on those domain events (i must use events because that same domain object can be managed by many loosely coupled views along with their viewmodels).
I also need to unregister to those events when that particular context is hidden.
I can handle this register/unregister/dispose in parallel with show/hide/dispose of that view using databinding, programmatically or whatever if the scenario keeps simple enough...
The problem comes when visualization logic comes with DataTemplates.
How can I know when that datatemplate becomes hidden so that I can unregister my events? is it there a better way with wpf to handle this, instead of adding more events?
What is the best practice to handle this scenario in a good MVVM approach?
edit: ok, the problem is structural. sometimes choices made inside the project has forced us to work in an atypical manner... in a good mvvm approach this problem should not happen
I'd be careful in making the ViewModel dependent on the View for making things right.
So what I would do is provide a property (Show? Visible? Open?) on the ViewModel that has a TwoWay binding with the View so the ViewModel can monitor the property.
I'm using EntityFramework for data access and wpf as UI. If I bind WPF components to navigation properties of my entity classes(usually EntityCollection<EntityClass>, exposed as IList<T> in service layer), UI is not updating the changes. I know i have to use ObservableCollection<T> or such, but I need some guidance on how to use it without iterating back and forth upon save and retrieval processes.
(As you guessed, I'm new to WPF; so target your answers for a WPF beginner)
You don't have to use ObservableCollection. WPF actually depends upon INotifyCollectionChanged, which ObservableCollection implements. So if you create a wrapper collection which implements this interface and forwards the operations onto the EntityCollection and raises the events, you should be good (as long as you modify the collection via the wrapper and not the underlying collection. A similar concept is used for read-only collections (wrap an existing collection and interact with wrapper), simple Decorator pattern.
You can't use it directly (and have the changes be reflected).
Here is a link that explains how someone else solved this problem
I faced the same problem in Silverlight LOB applications
I've created a silverlight library called ObservableCollections accompanied with visual studio 2012 addin and NUGet support, to generate the boilerplate code in order to wrap the EntityCollection with ObservableEntityCollection class, I know your question is about WPF but it could help.
http://observableec.codeplex.com/
Short question:
Do any of MS's built in Data Objects support INotifyPropertyChanged?
Long explination:
So I'm going to be displaying alot of data with databound controls.
The data is going to be chaging somewhat frequently with user interaction.
The application is a basic windows form app.
Rather than wire up events for all the data to the display controls I'm hoping that I can use data objects that implement INotifyPropertyChanged, that way the controls don't need to know the how, when or why their data changed just that they need to update themselves.
Sanity check:
Am I even barking up the right tree here?
The point of INotifyPropertyChange is to report property changes. To that extent, it's supposed to be implemented by specific model classes, not by general-purpose data objects. A more general solution for such objects is provided in form of PropertyDescriptor.AddValueChanged - since PropertyDescriptors can represent "virtual" properties, such as DataRow fields, or WPF attached properties.
I have been working for a few months on a rather large windows forms app, and we are using DataBinding and INotifyPropertyChanged for everything. It works really well, and I have no real problems to report. We are using our own classes, because there really isn't a data layer in this application, so I don't know for sure about the MS data classes.
BindableCollection< T > implements INotifyPropertyChanged