Hi, Instead of extending INotifyPropertyChanged to every viewModels. we can keep one "viewModelBase" which extends INotifyPropertyChanged and implement RaisePropertyChanged in it. Then extend this "viewModelBase" to all viewModel. Please let me know if the above way is not right.
Actually you dont neet INotifyPropertyChanged in the model or viewmodel for this example. The value of the person model is always changed from the input, so you dont need to send the property change notification to the target. Also since we are using ObservableCollection, any update to the collection will be notified to the UI.
Thank you for this video, there was some useful content and it was clearly explained. I think what is causing confusion however, and producing the behavior you “fix” but removing the notification interface from your model, is that you are only working with a single instance of Person within your view model. Each “Add” operation adds an additional reference to the same Person object whereas for your intention, you should add a copy of your input Person. OR you could have Person be a value type (struct) to ensure you are adding by value not reference. This is the reason all names change in the table when you modify the text boxes; every item is the same object. I may be wrong, I didn’t try the code, I’ve only watched the video. Many Thanks
Hi, You mentioned that ObservableCollection will notify the UI when there is change. Then is it required to have OnPropertyChanged method inside it? what is the use of it?
Hi EL, i'm trying to add one text box and bind it with name of selected items in the list view. But, unable to do it. Could you try it and let me know how to do it.
very wrong approach, only you should implement change listener onto your ViewModel, not on your model, you are killing the basic principle of Modularität und responsibility.
There are even cases where you're not able to change the model yourself at all, e.g. when using 3rd party objects. Only the view model is always under your complete control.
I am also getting a NullRefrenceException in the If logic in CanSubmitExecute in the PersonViewModel. i have also Instantiated the PersonViewModel via constructor
Just add the constructor in PersonViewModel class which initalizes the field _person which is of type Person and _persons which of type ObservableCollection. public PersonViewModel() { this._person=new Person(); this._persons=new ObservableCollection();}
Why are we Binding Person.Fname in XAML ?? while explaining the concept u said there is not connection between model and view but you are accessing class from model (i.e. person) and binding it with UI . just a doubt ! any one with the answer can help. thanks in advance !
Yes , view knows viewmodel and viewmodel knows model. There is direct connection between view and model. But we are using viewmodel to connect both . In xaml we have defined binding contect(source) as view model. so from the view model we can able to access the model and its properties. Hence we can able to get person properties in xaml(view) I hope it's helpful
@Super Nova, I was confused about the use of INotifyPropertyChanged in both the model and view model. Looks like we don't even use it... However... If the code is removed from the Person.cs file, then the GUI updates our instance in the back end, and screws up the list. Every entry gets whatever is currently in the text boxes. This feels wrong.
See, later you commented OnPropertyChanged event from properties of model, now since u commented it and now if you remove InotifyPropertyChanged Interface from class, your application will not work properly can you tell why ?
you have been told that the model is not used in the UI, but shown in the xaml binding of Fname, which is the property of the model. How are you using that
Why would you implement INotifyPropertyChanged on a model? How you gonna reuse that model or share it among other applications (or your data access layer) if it's coupled to WPF specific interface? INotifyPropertyChanged should be implemented only in ViewModel where you can use your pure models and wrap them within ViewModel's properties. This is how you expose them to UI, while they still don't know anything about the UI. PS: please read this thread: stackoverflow.com/questions/4453352/what-goes-in-model-in-mvvm/4453396#4453396
Hi, Can you please share video based on CURD operation in MVVM. 1.Curd operation in Datagrid (select any row in Datagrid, grid selected row values should show in respective text boxes, then we can change/update values then save/update, save values should reflect in Datagrid... 2. How we can go for the same approach when we want to store in Database. Thanks. Your all video is excellent.
Hello sir, I'm trying to implement this same but stuck somewhere.. While using the datacontext in grid. I'm facing a basic error which is not yet resolved everything is okay with the code but not understand why it's happening.. Error: object reference not set to an instance of an object. Please help if any suggestion
I had a same problem like you. For a short moment on 21:06 you can see the definition private ICommand _SubmitCommand; He made a change during a visible cut in the video but unfortunately he did not mention that. I hope this helps! :)
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using _21V_WPF_MVVM.Command; using _21V_WPF_MVVM.Model; namespace _21V_WPF_MVVM.ViewModel { public class PersonViewModel : INotifyPropertyChanged { private Person _person; public Person Person { get { return _person; } set { _person = value; NotifyPropertyChanged("Person"); } } public PersonViewModel() { Person = new Person(); PersonS = new ObservableCollection(); } private ObservableCollection _PersonS; public ObservableCollection PersonS { get { return _PersonS; } set { _PersonS = value; NotifyPropertyChanged("PersonS"); } } private ICommand _SubmitCommand; public ICommand SubmitCommand { get { if (_SubmitCommand == null) { _SubmitCommand = new RelayCommand(SubmitExecute, CanSubmitExecute, false); } return _SubmitCommand; } } private void SubmitExecute(object parameter) { PersonS.Add(Person); } private bool CanSubmitExecute(object parameter) { if (string.IsNullOrEmpty(Person.FName) || string.IsNullOrEmpty(Person.LName)) { return false; } else { return true; } } public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } }
In the same exact moment he pasted two methods, named SubmitExecute(object parameter) and CanSubmitExecute(object parameter) below the SubmitCommand property.
you are not define what is the type of it ( canExecuteCache = canExecuteCache; ) and i receive this type of error when i run your code. Object reference not set to an instance of an object. Help me if you can . Thank you
I dind´t get some thing plese help. If your application is going to work with a list of people that list should be part of the model or the view model ? In this video i saw that the teacher create the list in the view model... i don´t kown anything about databases but im going to star entity framework to bild an app but how is going to work with this ??? how we are going to pass a list to the view model to show it on the view please help...
object _person and _persons are not properly initialized in ModelView Class. use to following to update code private Person _person = new Person(); and private ObservableCollection _persons = new ObservableCollection();
I have one doubt. Did not you create object of model in ViewModel ? Because its not working at all if we dont initialize the object of model in ViewModel and I dont see where you are doing this, please reply on this.
This example looks incomplete. In ViewModel class you need to define FName and LName properties which you missed. This properties from ViewModel will access Model class properties. Please confirm.
Hi sir, This channel is great to learn, I am doing the same as per this but my button is disabled and after a click on the button it will throw the NullReferenece Exception I have set DataContext to Grid which is in window Resource static mentioned. please suggest how do I fix this ???
When we say Model shouldn't talk to view then why INotifyPropertyChanged implemented in model? Aren't we trying to notify the UI from Model? Refer to this stackoverflow.com/questions/5027562/the-role-of-the-model-in-mvvm
Valid question, but the sole purpose of MVVM is to achieve decoupling. Now when we implement this interface the change notification is sent as an event which gives us decoupling. There is also a way to implement in view model that is done by MVVM purists. But problem with that approach is VM is the class where we have our business logic and implementing change notification event could violate Single responsibility principle. Hence generally change notification is implemented through Model in general practice.
You should not implement it on your Model but only on your ViewModel, and then VM will talk with your model, and VM you have already bound to main application.
Hello Sir actually my parameter is null . And i am getting stack overflow Exception. Here Can execute is giving null and window is not opening how can i solve that?
when adding data in textbox and clicking then showing in Listview and when again entering data in textbox then even the old entries in listview changing
Hi you are making awesome videos. I have a question about architecture. When you want to work with collections u made model of person and then obsevablecolletion of persons in view model, shouldn't you make collection of persons as model class and then create viewmodel of that collection? Because when you want to work with Persons in backted you only get viewmodel to edit. So if I unterstod it right u work with model classes in you app background and use INotifyPropertyChange to propagate those changes to ViewModel and then GUI. Thanks!
You are right.. Coming to yr first query.. What happens if we need to change the model, hence we keep our models or entities seperate and implement it's collection in viewModel.. The approach you thought was also correct but MVVM approaches differ across developers. This is the reason why microsoft has not released any stable template of MVVM in visual studio.. Only toolkits like MVVM light exists from third party. My approach was simple to make everyone understand.. Great point though....
Hi, If anyone knows solution for "the specified class doesn't exist in given namespace" error in WPF - MVVM. Please ping here. Dotnetskool .. do you have a answer ?
ravi joshi by passing parent viewmodel's reference to chid view model. Untill you are passing data between window's vm. 2) you can use event bus. Or 3) singleton will do the work for you 4) best of all use reactive programming
I am getting exception 'Object reference not to set an instance of an object' in MainWindow.xaml file plz help. And don't scroll up and down it was too fast. Give proper title to your videos and every time make new application or else you can give number 1,2 like this
@@sandeeppuvvadi1729 Hi, could you help me to add one more text box in the same code which tells the first name of the selected item in the list view.
I think u made it little complicated 🥺, I have watched all of your previous videos it was fine and I thought I understood it but in this video some details are dropped like a bomb n confused the hell out of me
Hi Sir, I am a New bee in .Net, I have tried to implement same example , due to some reason application getting crashed,can you please share the source code for your lecture it will really helpful. Thanks
Very good videos series on WPF. I passed the interview based on this series. Where to get the code of these videos? I am getting object to set to instance error at protected void NotifyProperyChanged(string propertyName) { if (propertyName != null) { PropertyChanged(this,new PropertyChangedEventArgs(propertyName)); } }
Got it working, here's my code: pastebin.com/cQ92tzjE Fixed null references by constructing instances of the private fields _person and _persons. - Made changes in the file PersonViewModel.cs: private Person _person; becomes: private Person _person = new Person(); private ObservableCollection _persons; becomes private ObservableCollection _persons = new ObservableCollection();
Hi,
Instead of extending INotifyPropertyChanged to every viewModels. we can keep one "viewModelBase" which extends INotifyPropertyChanged and implement RaisePropertyChanged in it. Then extend this "viewModelBase" to all viewModel.
Please let me know if the above way is not right.
i know it's kinda off topic but does anybody know of a good place to watch newly released tv shows online?
@Xander Alijah i would suggest FlixZone. You can find it by googling :)
@Cannon Toby definitely, I have been using flixzone for since april myself :)
@Cannon Toby thank you, I went there and it seems like they got a lot of movies there :D I appreciate it!
@Xander Alijah You are welcome :D
Actually you dont neet INotifyPropertyChanged in the model or viewmodel for this example. The value of the person model is always changed from the input, so you dont need to send the property change notification to the target. Also since we are using ObservableCollection, any update to the collection will be notified to the UI.
Thank you for this video, there was some useful content and it was clearly explained.
I think what is causing confusion however, and producing the behavior you “fix” but removing the notification interface from your model, is that you are only working with a single instance of Person within your view model. Each “Add” operation adds an additional reference to the same Person object whereas for your intention, you should add a copy of your input Person. OR you could have Person be a value type (struct) to ensure you are adding by value not reference. This is the reason all names change in the table when you modify the text boxes; every item is the same object.
I may be wrong, I didn’t try the code, I’ve only watched the video. Many Thanks
Hi,
You mentioned that ObservableCollection will notify the UI when there is change. Then is it required to have OnPropertyChanged method inside it? what is the use of it?
Thak you very much for this video, now i understand the concept about Command.
Hi EL, i'm trying to add one text box and bind it with name of selected items in the list view. But, unable to do it. Could you try it and let me know how to do it.
very wrong approach, only you should implement change listener onto your ViewModel, not on your model, you are killing the basic principle of Modularität und responsibility.
There are even cases where you're not able to change the model yourself at all, e.g. when using 3rd party objects. Only the view model is always under your complete control.
I am getting NullPointer exception in string.isnullorempty(Person.FName), i tried exactly same as you did. Where are we create Person Object ?
do it this way, private Person _person = new Person();
I m getting null reference exception while binding view model to grid . pls suggest
same here
Pls mail me yr id at mailid shown on video..
u have not mention ur mail id in video...cant find it.....plz give it here.......can u plz send me the project on vivekbsnl30@gmail.com
I am also getting a NullRefrenceException in the If logic in CanSubmitExecute in the PersonViewModel. i have also Instantiated the PersonViewModel via constructor
Just add the constructor in PersonViewModel class which initalizes the field _person which is of type Person and _persons which of type ObservableCollection. public PersonViewModel()
{
this._person=new Person();
this._persons=new ObservableCollection();}
Why are we Binding Person.Fname in XAML ?? while explaining the concept u said there is not connection between model and view but you are accessing class from model (i.e. person) and binding it with UI . just a doubt ! any one with the answer can help. thanks in advance !
Yes , view knows viewmodel and viewmodel knows model. There is direct connection between view and model.
But we are using viewmodel to connect both . In xaml we have defined binding contect(source) as view model. so from the view model we can able to access the model and its properties. Hence we can able to get person properties in xaml(view)
I hope it's helpful
@Super Nova, I was confused about the use of INotifyPropertyChanged in both the model and view model.
Looks like we don't even use it... However...
If the code is removed from the Person.cs file, then the GUI updates our instance in the back end, and screws up the list. Every entry gets whatever is currently in the text boxes.
This feels wrong.
why did you directly bind your text boxes from view to model?? is that an option to relate view and model in this way?
See, later you commented OnPropertyChanged event from properties of model, now since u commented it and now if you remove InotifyPropertyChanged Interface from class, your application will not work properly can you tell why ?
you have been told that the model is not used in the UI, but shown in the xaml binding of Fname, which is the property of the model. How are you using that
Why would you implement INotifyPropertyChanged on a model? How you gonna reuse that model or share it among other applications (or your data access layer) if it's coupled to WPF specific interface? INotifyPropertyChanged should be implemented only in ViewModel where you can use your pure models and wrap them within ViewModel's properties. This is how you expose them to UI, while they still don't know anything about the UI. PS: please read this thread: stackoverflow.com/questions/4453352/what-goes-in-model-in-mvvm/4453396#4453396
You are correct, this tutorial is wrong.
Hi, Can you please share video based on CURD operation in MVVM.
1.Curd operation in Datagrid (select any row in Datagrid, grid selected row values should show in respective text boxes, then we can change/update values then save/update, save values should reflect in Datagrid...
2. How we can go for the same approach when we want to store in Database.
Thanks.
Your all video is excellent.
Hello sir, I'm trying to implement this same but stuck somewhere.. While using the datacontext in grid. I'm facing a basic error which is not yet resolved everything is okay with the code but not understand why it's happening..
Error: object reference not set to an instance of an object. Please help if any suggestion
Dear Sir,
Can you provide the source code,
I have a problem in _SubmitCommand
It does not exist.Please help me in this regard
I had a same problem like you. For a short moment on 21:06 you can see the definition
private ICommand _SubmitCommand;
He made a change during a visible cut in the video but unfortunately he did not mention that. I hope this helps! :)
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using _21V_WPF_MVVM.Command;
using _21V_WPF_MVVM.Model;
namespace _21V_WPF_MVVM.ViewModel
{
public class PersonViewModel : INotifyPropertyChanged
{
private Person _person;
public Person Person
{
get { return _person; }
set
{
_person = value;
NotifyPropertyChanged("Person");
}
}
public PersonViewModel()
{
Person = new Person();
PersonS = new ObservableCollection();
}
private ObservableCollection _PersonS;
public ObservableCollection PersonS
{
get { return _PersonS; }
set
{
_PersonS = value;
NotifyPropertyChanged("PersonS");
}
}
private ICommand _SubmitCommand;
public ICommand SubmitCommand
{
get
{
if (_SubmitCommand == null)
{
_SubmitCommand = new RelayCommand(SubmitExecute, CanSubmitExecute, false);
}
return _SubmitCommand;
}
}
private void SubmitExecute(object parameter)
{
PersonS.Add(Person);
}
private bool CanSubmitExecute(object parameter)
{
if (string.IsNullOrEmpty(Person.FName) || string.IsNullOrEmpty(Person.LName))
{
return false;
}
else
{
return true;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
hi dude, u must define it : private ICommand _SubmitCommand;
then : public ICommand SubmitCommand { … }
in PersonViewModel class
21:09 how the error was removed from _SubmitCommand
Can anyone explain?
In the same exact moment he pasted two methods, named SubmitExecute(object parameter) and CanSubmitExecute(object parameter) below the SubmitCommand property.
@@deadlock107 thank you
you are not define what is the type of it ( canExecuteCache = canExecuteCache; ) and
i receive this type of error when i run your code. Object reference not set to an instance of an object.
Help me if you can . Thank you
I dind´t get some thing plese help.
If your application is going to work with a list of people that list should be part of the model or the view model ?
In this video i saw that the teacher create the list in the view model...
i don´t kown anything about databases but im going to star entity framework to bild an app but how is going to work with this ??? how we are going to pass a list to the view model to show it on the view please help...
Could you please upload source code as i am getting null reference exception while binding view model to Grid
object _person and _persons are not properly initialized in ModelView Class. use to following to update code
private Person _person = new Person();
and
private ObservableCollection _persons = new ObservableCollection();
@@TheBhai420 Thank you, this solved it for me!
@@TheBhai420 thank you this solved my problem..
I have one doubt. Did not you create object of model in ViewModel ? Because its not working at all if we dont initialize the object of model in ViewModel and I dont see where you are doing this, please reply on this.
You didn't made the object of Person in PersonViewModel so how come your application didn't return NULL exception ?
Hi, Can you please add 2 more text box and bind those text box with the name of selected items in the list view.
problem with canExecuteCache?
can you please share the relaycommand code?
This example looks incomplete. In ViewModel class you need to define FName and LName properties which you missed. This properties from ViewModel will access Model class properties. Please confirm.
Hi sir, This channel is great to learn, I am doing the same as per this but my button is disabled and after a click on the button it will throw the NullReferenece Exception I have set DataContext to Grid which is in window Resource static mentioned. please suggest how do I fix this ???
In the" personviewmodel.cs" im getting "_SubmitCommand" error. plz help...
i have posted in two post bcoz of youtube restriction of long post.
I want to know what is the best approach error handling in MVVM?
IDataError Info interface if you are working with MVVM
When we say Model shouldn't talk to view then why INotifyPropertyChanged implemented in model? Aren't we trying to notify the UI from Model? Refer to this stackoverflow.com/questions/5027562/the-role-of-the-model-in-mvvm
Valid question, but the sole purpose of MVVM is to achieve decoupling. Now when we implement this interface the change notification is sent as an event which gives us decoupling. There is also a way to implement in view model that is done by MVVM purists. But problem with that approach is VM is the class where we have our business logic and implementing change notification event could violate Single responsibility principle. Hence generally change notification is implemented through Model in general practice.
You should not implement it on your Model but only on your ViewModel, and then VM will talk with your model, and VM you have already bound to main application.
Hello Sir actually my parameter is null . And i am getting stack overflow Exception. Here Can execute is giving null and window is not opening how can i solve that?
when adding data in textbox and clicking then showing in Listview and when again entering data in textbox then even the old entries in listview changing
You kept scrolling up and down the screen too damn fast it makes it hard to follow.
Good job..👍
Hi you are making awesome videos. I have a question about architecture. When you want to work with collections u made model of person and then obsevablecolletion of persons in view model, shouldn't you make collection of persons as model class and then create viewmodel of that collection? Because when you want to work with Persons in backted you only get viewmodel to edit. So if I unterstod it right u work with model classes in you app background and use INotifyPropertyChange to propagate those changes to ViewModel and then GUI.
Thanks!
You are right.. Coming to yr first query.. What happens if we need to change the model, hence we keep our models or entities seperate and implement it's collection in viewModel.. The approach you thought was also correct but MVVM approaches differ across developers. This is the reason why microsoft has not released any stable template of MVVM in visual studio.. Only toolkits like MVVM light exists from third party. My approach was simple to make everyone understand..
Great point though....
Hello, Please add Source code text in description as this one is bit complex, to follow. If source is available for download far better :)
Thanks
Sir , Kindly explain also if we have multiple buttons on a form . how to managed command for each button in MVVM ?
Build separate logic for command action, and hook up in the same way you did for first button.. Try it, will happy to help if u dnt succeed
I didn't get the purpose of canexecutecache ? Am I the only one missing it or it's never used ?
plz add demo of using SQL databases in mvvm
please explain dependency injection in wpf
extremely helpfull... thnx a lot
Unable to understand, where data is going since You are not using sql server.
Hi,
If anyone knows solution for "the specified class doesn't exist in given namespace" error in WPF - MVVM.
Please ping here.
Dotnetskool .. do you have a answer ?
I an getting the same error. Did you get the answer?
Request you to please share one video on "How to communicate between two view model" .
ravi joshi by passing parent viewmodel's reference to chid view model. Untill you are passing data between window's vm.
2) you can use event bus.
Or
3) singleton will do the work for you
4) best of all use reactive programming
@@keshavejat Hi, could you help me to add one more text box in the same code which tells the first name of the selected item in the list view.
I am getting exception 'Object reference not to set an instance of an object' in MainWindow.xaml file plz help. And don't scroll up and down it was too fast.
Give proper title to your videos and every time make new application or else you can give number 1,2 like this
add a constructor in the PersonViewModel class: public PersonViewModel(){ this.Person=new Person(); this.Persons=new ObservableCollection();}
I am new to Binding. I get NullReferenceException by binding
oh . I just realize I never initialize person = new Person();
Hey @@joshuasafabre1202 where did you initialize it??/ even i am getting the same error
@@sandeeppuvvadi1729 Hi, could you help me to add one more text box in the same code which tells the first name of the selected item in the list view.
I think u made it little complicated 🥺, I have watched all of your previous videos it was fine and I thought I understood it but in this video some details are dropped like a bomb n confused the hell out of me
Please add a demo of using sqlite data base using mvvm
can you make video on mvvm example?
Hello Sir, please send me a code of this tutorial.
Hi Sir, I am a New bee in .Net, I have tried to implement same example , due to some reason application getting crashed,can you please share the source code for your lecture it will really helpful. Thanks
please upload the source code of above application
Adds really kill this tutorial....
very good video, how would i delete the added person or clear all from this listview?
Very good videos series on WPF. I passed the interview based on this series. Where to get the code of these videos? I am getting object to set to instance error at protected void NotifyProperyChanged(string propertyName)
{
if (propertyName != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}
}
Got it working, here's my code:
pastebin.com/cQ92tzjE
Fixed null references by constructing instances of the private fields _person and _persons.
- Made changes in the file PersonViewModel.cs:
private Person _person;
becomes:
private Person _person = new Person();
private ObservableCollection _persons;
becomes
private ObservableCollection _persons = new ObservableCollection();
Thank you so much, this helped us soooooo much. Thank you
@@meghnatiwari2527 You're welcome :)
Hi, could you help me to add one more text box in the same code which tells the first name of the selected item in the list view.
Hi bro how can i contact you? AFTER WPF,MVVM we didn't receive any video from your end. you can teach us PRISM?
please upload the source code of above application