What you are doing is mostly fine - though I would personally make the SelectedServiceCategory a "real" property (with a value that's saved).
Up vote 2 down vote favorite share g+ share fb share tw.
I have 2 listBoxes, if you click an item in the top one, then the bottom one filters to a few results. I am trying to learn WPF and MVVM and am wondering if this is the correct way to do this. Is this the best way?
Here is what I did: class VisitInfoViewModel : ViewModelBase { List serviceTypes; List allServiceTypes; public VisitInfoViewModel() { ServiceCategories = ServiceCategory. Categories; allServiceTypes = ServiceType. ServiceTypes; } public List ServiceCategories { get; set; } public List ServiceTypes { get { return serviceTypes; } } public ServiceCategory SelectedServiceCategory { get { return null; } set { serviceTypes = allServiceTypes.
FindAll(st => st. ServiceCategoryGuid. Equals(value.
Guid)); OnPropertyChanged("ServiceTypes"); } } } and MainWindow. Xaml snippet also, why shouldn't I just add an EventHandler for selectedItemChanged on my listBox? It seems so much simpler and clearer to use the event handler.
I think it is because if I did that it would no longer by MVVM... is that correct? What would you do and what are the best practices? Wpf data-binding mvvm listbox link|improve this question edited Dec 9 '11 at 18:26H.B.43.4k61646 asked Dec 9 '11 at 17:58bebonham406 75% accept rate.
What you are doing is mostly fine - though I would personally make the SelectedServiceCategory a "real" property (with a value that's saved). The difference with MVVM, and doing it in code behind, is that you're working with data. If you make the "Current Category" change the types, then you're working purely with the data, and not worrying about the UI at all.
You can change the category by any mechanism, and the UI will always stay up to date. I, personally, would suggest writing this more like so: class VisitInfoViewModel : ViewModelBase { List allServiceTypes; public VisitInfoViewModel() { ServiceCategories = ServiceCategory. Categories; allServiceTypes = ServiceType.
ServiceTypes; } // This can use a private setter... public IEnumerable ServiceCategories { get; private set; } private ServiceCategory currentCategory; public ServiceCategory CurrentServiceCategory { get { return this. CurrentCategory; } set { if (this. CurrentCategory!
= value) { this. CurrentCategory = value; ServiceTypesInCurrentCategory = allServiceTypes. Where(st => st.
ServiceCategoryGuid. Equals(this.currentCategory. Guid)); OnPropertyChagned("CurrentServiceCategory"); OnPropertyChanged("ServiceTypes"); } } } public IEnumerable ServiceTypesInCurrentCategory { get; private set; } } This provides complete freedom to change the CurrentServiceCategory in code or via Xaml, without any event handlers.
It also makes your ViewModel completely data related - you don't know or care what is being used to display this - as long as you have something in your View that sets the CurrentServiceCategory, everything stays synchronized correctly. Also, why shouldn't I just add an EventHandler for selectedItemChanged on my listBox? It seems so much simpler and clearer to use the event handler.
I think it is because if I did that it would no longer by MVVM... is that correct? You can do that, but it's typically a violation of MVVM at this point. The main issue is that you'd be coupling the implementation to that event handler - by doing this, you're basically "locking in" the behavior based on your code in your View for this specific implementation of the View.
By keeping it "pure" in terms of MVVM, you're View is free to change (ie: maybe you want to switch to a combobox for the ServiceCategories someday) without touching your ViewModel code at all...
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.