Тёмный

WPF MVVM Implementation 

DotNetSkoool
Подписаться 12 тыс.
Просмотров 92 тыс.
50% 1

Hey Guys, In this video let us implement the MVVM pattern. It is a very simple application but the video is a bit long. But this is a fruitful half hour spent. Try to practise more and more and you will be a ninja.. :-)
Code at : dotnetskoool.blogspot.com/202...
Keep Learning.

Опубликовано:

 

16 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 112   
@georgekerwood9100
@georgekerwood9100 5 лет назад
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
@siddgoen
@siddgoen 2 года назад
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.
@karthikapandiyaraj4129
@karthikapandiyaraj4129 3 года назад
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?
@maxysexy69
@maxysexy69 7 лет назад
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.
@kimh.6680
@kimh.6680 4 года назад
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.
@abidmehmoods
@abidmehmoods 4 года назад
extremely helpfull... thnx a lot
@pundha1
@pundha1 5 лет назад
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.
@xanderalijah3708
@xanderalijah3708 3 года назад
i know it's kinda off topic but does anybody know of a good place to watch newly released tv shows online?
@cannontoby7273
@cannontoby7273 3 года назад
@Xander Alijah i would suggest FlixZone. You can find it by googling :)
@azariaheden2806
@azariaheden2806 3 года назад
@Cannon Toby definitely, I have been using flixzone for since april myself :)
@xanderalijah3708
@xanderalijah3708 3 года назад
@Cannon Toby thank you, I went there and it seems like they got a lot of movies there :D I appreciate it!
@cannontoby7273
@cannontoby7273 3 года назад
@Xander Alijah You are welcome :D
@amirmoein834
@amirmoein834 4 года назад
why did you directly bind your text boxes from view to model?? is that an option to relate view and model in this way?
@satyaprakashmishra6330
@satyaprakashmishra6330 7 лет назад
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.
@kalyanreddy1463
@kalyanreddy1463 6 лет назад
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
@mr.es1857
@mr.es1857 6 лет назад
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...
@muditsingh9885
@muditsingh9885 7 лет назад
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 ?
@elaprendiz7208
@elaprendiz7208 Год назад
Thak you very much for this video, now i understand the concept about Command.
@amathmat9307
@amathmat9307 Год назад
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.
@quantomic1106
@quantomic1106 4 года назад
You kept scrolling up and down the screen too damn fast it makes it hard to follow.
@muditsingh9885
@muditsingh9885 7 лет назад
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.
@me85638251
@me85638251 5 лет назад
@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.
@sameerrisbud780
@sameerrisbud780 6 лет назад
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?
@bhjo9386
@bhjo9386 7 лет назад
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
@testdata715
@testdata715 6 лет назад
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.
@yashmedappa3553
@yashmedappa3553 6 лет назад
I didn't get the purpose of canexecutecache ? Am I the only one missing it or it's never used ?
@sathishkomakul
@sathishkomakul 4 года назад
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 ???
@sanjaybharga8459
@sanjaybharga8459 7 лет назад
plz add demo of using SQL databases in mvvm
@ur67tube
@ur67tube 4 года назад
Good job..👍
@ratanmishra3513
@ratanmishra3513 7 лет назад
In the" personviewmodel.cs" im getting "_SubmitCommand" error. plz help...
@vaishnavisharma2492
@vaishnavisharma2492 2 года назад
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
@VIVEKKUMAR-mh9dc
@VIVEKKUMAR-mh9dc 7 лет назад
i have posted in two post bcoz of youtube restriction of long post.
@amathmat9307
@amathmat9307 Год назад
Hi, Can you please add 2 more text box and bind those text box with the name of selected items in the list view.
@motivationguru7155
@motivationguru7155 8 месяцев назад
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
@priyasutar8025
@priyasutar8025 5 лет назад
problem with canExecuteCache? can you please share the relaycommand code?
@usamaajmal2812
@usamaajmal2812 5 лет назад
You didn't made the object of Person in PersonViewModel so how come your application didn't return NULL exception ?
@elaith9
@elaith9 7 лет назад
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
@maxysexy69
@maxysexy69 7 лет назад
You are correct, this tutorial is wrong.
@yashidhar8574
@yashidhar8574 6 лет назад
can you make video on mvvm example?
@PAJANI1910
@PAJANI1910 4 года назад
I am getting NullPointer exception in string.isnullorempty(Person.FName), i tried exactly same as you did. Where are we create Person Object ?
@moropaneedwin4915
@moropaneedwin4915 2 года назад
do it this way, private Person _person = new Person();
@adeelashraf018
@adeelashraf018 4 года назад
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
@bntdgp
@bntdgp 5 лет назад
Unable to understand, where data is going since You are not using sql server.
@Kamza_
@Kamza_ 7 лет назад
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!
@dotnetskoool5809
@dotnetskoool5809 7 лет назад
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....
@qingchen9065
@qingchen9065 4 года назад
add a constructor in the PersonViewModel class: public PersonViewModel(){ this.Person=new Person(); this.Persons=new ObservableCollection();}
@anushag239
@anushag239 7 лет назад
I m getting null reference exception while binding view model to grid . pls suggest
@VIVEKKUMAR-mh9dc
@VIVEKKUMAR-mh9dc 7 лет назад
same here
@dotnetskoool5809
@dotnetskoool5809 7 лет назад
Pls mail me yr id at mailid shown on video..
@VIVEKKUMAR-mh9dc
@VIVEKKUMAR-mh9dc 7 лет назад
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
@VIVEKKUMAR-mh9dc
@VIVEKKUMAR-mh9dc 7 лет назад
I am also getting a NullRefrenceException in the If logic in CanSubmitExecute in the PersonViewModel. i have also Instantiated the PersonViewModel via constructor
@androidproject2617
@androidproject2617 7 лет назад
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();}
@ReddiSingala
@ReddiSingala 7 лет назад
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
@BhaveshAhir89
@BhaveshAhir89 7 лет назад
Hello Sir, please send me a code of this tutorial.
@jenishtitus424
@jenishtitus424 4 года назад
please explain dependency injection in wpf
@saimadhuripadarthi504
@saimadhuripadarthi504 3 года назад
Please add a demo of using sqlite data base using mvvm
@arvindkumarsingh7429
@arvindkumarsingh7429 7 лет назад
I want to know what is the best approach error handling in MVVM?
@dotnetskoool5809
@dotnetskoool5809 7 лет назад
IDataError Info interface if you are working with MVVM
@sunny3831
@sunny3831 3 года назад
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 !
@rachaputisrikanth1243
@rachaputisrikanth1243 2 года назад
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
@anushag239
@anushag239 7 лет назад
Could you please upload source code as i am getting null reference exception while binding view model to Grid
@TheBhai420
@TheBhai420 5 лет назад
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();
@JohnXs233
@JohnXs233 4 года назад
@@TheBhai420 Thank you, this solved it for me!
@hitikchauhan9582
@hitikchauhan9582 4 года назад
@@TheBhai420 thank you this solved my problem..
@ravi1410a
@ravi1410a 7 лет назад
Request you to please share one video on "How to communicate between two view model" .
@keshavejat
@keshavejat 6 лет назад
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
@amathmat9307
@amathmat9307 Год назад
@@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.
@NisarAhmad44
@NisarAhmad44 7 лет назад
Sir , Kindly explain also if we have multiple buttons on a form . how to managed command for each button in MVVM ?
@dotnetskoool5809
@dotnetskoool5809 7 лет назад
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
@jonestako4721
@jonestako4721 6 лет назад
please upload the source code of above application
@sawlalit
@sawlalit 7 лет назад
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
@dotnetskoool5809
@dotnetskoool5809 7 лет назад
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.
@maxysexy69
@maxysexy69 7 лет назад
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.
@anandraj4061
@anandraj4061 6 лет назад
Dear Sir, Can you provide the source code, I have a problem in _SubmitCommand It does not exist.Please help me in this regard
@valentinoskobljanec930
@valentinoskobljanec930 5 лет назад
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! :)
@jonestako4721
@jonestako4721 5 лет назад
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)); } } } }
@the_null
@the_null 5 лет назад
hi dude, u must define it : private ICommand _SubmitCommand; then : public ICommand SubmitCommand { … }
@the_null
@the_null 5 лет назад
in PersonViewModel class
@aniketmarulkar8491
@aniketmarulkar8491 3 года назад
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
@richardprakash4630
@richardprakash4630 3 года назад
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
@pundha1
@pundha1 5 лет назад
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 ?
@omyerawar7976
@omyerawar7976 2 года назад
I an getting the same error. Did you get the answer?
@GamerPolygon
@GamerPolygon 6 лет назад
very good video, how would i delete the added person or clear all from this listview?
@wh0urdady
@wh0urdady 5 лет назад
Adds really kill this tutorial....
@joshuasafabre1202
@joshuasafabre1202 5 лет назад
I am new to Binding. I get NullReferenceException by binding
@joshuasafabre1202
@joshuasafabre1202 5 лет назад
oh . I just realize I never initialize person = new Person();
@sandeeppuvvadi1729
@sandeeppuvvadi1729 5 лет назад
​Hey @@joshuasafabre1202 where did you initialize it??/ even i am getting the same error
@amathmat9307
@amathmat9307 Год назад
​@@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.
@mohammedusman3581
@mohammedusman3581 3 года назад
21:09 how the error was removed from _SubmitCommand Can anyone explain?
@deadlock107
@deadlock107 3 года назад
In the same exact moment he pasted two methods, named SubmitExecute(object parameter) and CanSubmitExecute(object parameter) below the SubmitCommand property.
@mohammedusman3581
@mohammedusman3581 3 года назад
@@deadlock107 thank you
@waqarjamil7450
@waqarjamil7450 7 лет назад
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)); } }
@me85638251
@me85638251 5 лет назад
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();
@meghnatiwari2527
@meghnatiwari2527 3 года назад
Thank you so much, this helped us soooooo much. Thank you
@me85638251
@me85638251 3 года назад
@@meghnatiwari2527 You're welcome :)
@amathmat9307
@amathmat9307 Год назад
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.
@kithiyongideon1629
@kithiyongideon1629 2 года назад
Hi bro how can i contact you? AFTER WPF,MVVM we didn't receive any video from your end. you can teach us PRISM?
@surajbhansingh2220
@surajbhansingh2220 6 лет назад
please upload the source code of above application
Далее
Value Converters in WPF
14:50
Просмотров 28 тыс.
Dispatcher in WPF
19:12
Просмотров 37 тыс.
меня не было 9 дней
12:48
Просмотров 2,3 млн
Commands In WPF
20:34
Просмотров 76 тыс.
List Vs ObservableCollection in WPF
13:35
Просмотров 45 тыс.
IEnumerable and IEnumerator Interface in C#
13:58
Просмотров 25 тыс.
WPF DataGrid Demo
20:46
Просмотров 32 тыс.
Data Binding Modes in WPF
12:51
Просмотров 93 тыс.
WPF in C# with MVVM using Caliburn Micro
1:23:30
Просмотров 361 тыс.