Reputation: 2940
I think this question could of already been answered, which if this is the case I will delete immediately. I'm new to WPF (and new to C# really) and I'm trying as hard as possible to follow MVVM but I'm really struggling with finding a way to NOT implement INotifyPropertyChanged
on my Model.
My Model contains a collection of predefined objects which are created from another reference. Within the Model, I have properties which are updated as and when the Task
is completed.
CustomObject - API
public class CustomObject
{
private string _myTaskOutput;
public CustomObject()
{
_myTaskOutput = MyTaskMethod();
}
//MyTaskMethod()
//Get & Setter for _myTaskOutput;
}
Util - API
public static class Util
{
public IEnumerable<CustomObject> GenerateObjects()
{
var customObjectCollection = SomeHelperMethod();
return customObjectCollection;
}
}
Model
public class CustomObjectCollectionModel
{
private IEnumerable<CustomObject> _customObjectCollection;
public CustomObjectCollectionModel()
{
_customObjectCollection = Util.GenerateObjects();
}
//Get & Setter for IEnumerable<CustomObject>
}
The object reference does not change so I believe having an ObservableCollection
is out of the question (is this correct?) and implementing INotifyPropertyChanged
on CustomObject
works as intended but I understand this not the correct way of doing things.
Upvotes: 0
Views: 163
Reputation: 169200
If CustomObject
is some kind of domain or business object, you could wrap it in a view model class in your client application. So instead of binding to an ObservableCollection<CustomObject>
you bind to an ObservableCollection<CustomObjectWrapper>
where CustomObjectWrapper
is a client-specific class that implements the INotifyPropertyChanged
interface (provided that you actually need to provide change notifications in your UI).
You will indeed have to create a CustomObjectWrapper
for each CustomObject
that you receive from your service API - you could do this in your view model class - but at the same time you don't have to pollute your business objects with client-specific code. Binding directly to domain-specific business objects is rarely useful.
Upvotes: 1