Master View implementation using MVVM in Silverlight - silverlight

I have a silverlight datagrid to which I am binding an observable collection from the viewmodel. There is a detail view page which will display different properties of the object in the collection when user selects a row of the datagrid. My requirement is when user updates any properties in the detail view; the data should be updated in the data grid also. How to implement this functionality?

Well, the answer is simply to bind both the datagrid row and the control displaying the selected object. The simplest way is to use a ICollectionView (returned by a CollectionViewSource from the original ObservableCollection), bind the grid's ItemsSource to that, and then bind the control's DataContext to the ICollectionView's CurrentItem. That way, when the grid's selected item changes, the CurrentItem of the ICollectionView is updated, and that item is displayed in the detail view.
I think it's quite easy but if you need additional details or sample source code I'll elaborate.

Related

Selecteditem event in MVVM silverlight

i have a datagrid bound to a property. In this grid i have columns which consists of cells which are like hyperlink i mean when user clicks on the cell value based on these values another gird will get populated. i want to know how to get the cell value and pass it to some method so that other grid will get populated.
The best way to do this is in your viewmodel.
You should bind the SelectedItem of your datagrid to a new property in your ViewModel. In the set method of this new Property, call a new method to populate a new ObservableCollection/List/whatever...
Finally, bind your "other grid" ItemsSource to this new observable collection from your ViewModel.
Edit:
If you need to load one thing or another depending on the column you are going to use the code behind, take a look at this:
Silverlight DataGrid how to get cell value from a selected item?

Refresh a DataGrid in the MVVM pattern

I have a View with a dataGrid. This datagrid bind a property in the ViewModel that is an ObservableCollection.
I edit some data in the dataGrid, and a field is updated by code, because it depends on some operations. Well, if I check the item in the observable collection, I can see that all the data is correct, but the info in the dataGrid is no refresh.
I want to force the refresh because I know that the observableCollection only raise the change property event when I add o remove items, but not if I edit one of them.
Because I am use Entity Framework 4.1, really the ItemsSource of the dataGrid is the local of the DbSet, so I don't know how to implement the notifyPorpertyChanged in the classes of the model edmx, and I am looking for an alternative, like to force refresh the dataGrid.
Because the property of the ViewModel that I use to bing the ItemsSource of the dataGrid is a reference to the local, I mean that to set the property I do myProperty = myContext.MyTable.Local and that raise the event PropertyChanged that I implement in my ViewModel, I try to do myProperty = myContext.MyTable.Local again to try to raise the event and force the refresh of the dataGrid, but it does not work.
What alternatives do I have?
Make sure that you have the Binding Mode set to TwoWay. Implement in the set portion of your property OnPropertyChanged and the rest should take care of itself.

Bind dynamic value coming from GUI to the viewmodel

There is an itemscontrol in one of my views that lets the user reorder the items by an up/down button.
My question is on how I can flow the current index of the items to my viewmodel
The following questions address on how to find the current index (not so clean but it does the job)
WPF - Bind to Item Index from within ItemTemplate of ItemsControl?
Now, how can I modify the XAML so that it binds this index to a property on my viewmodel?
edit The question is about how to do it in XAML declaratively. A possible solution is to do it in code using and ObservableCollection and subscribe to CollectionChanged
Kind Regards, Tom
So your items store an index poperty that you need to update when they are re-ordered in the UI?
I would create a view model that exposes my items as an ObservableCollection. You can then handle the CollectionChanged event which will fire when the items are re-ordered in your view model. At this point you can enumerate the collection of items updating their indices.

"Nested" MVVM Question

I have a WPF window that has a datagrid and a user control for a form for the fields in that datagrid. The user control and the WPF window have view models.
The user control's DataContext is bound to one of the window's view model's member field, whose value changes during the data grid's Selection Changed event.
I'm not sure if this is the right way to do it because I am unable to create references from the inner view model to the outer view model for some reason. Constructor injection won't work because I'm required to use default constructor only, and I can't seem to put a property injector in the right place (always getting null reference when I try to use it).
I am also unable to get my property change notification to work properly in the inner view model.
Is there a better way to wire my view models so that they automatically change the values in the user control when a new row is selected in the datagrid? I have a feeling that binding to the control's DataContext is not the way to go.
This doesn't seems a complex/nested scenario. Looks like pretty much a normal master details scenario. Suppose you want to edit Customer data, I would have an ObservableCollection instance bind to the DataGrid, and there will be a SelectedCustomer property in the VM also. In the DataGrid you can set SelectedItem twoway bind to the SelectedCustomer property which makes SelectedCustomer always updated with your selection. Since the usercontrol has the same instance of customer as in the DataGrid row, whenever you change anything in the UC those data will get reflected in the grid. Ofcourse all those properties should fire NotifypropertyChanged.

Data Binding with WPF and MVVM/Model-View-ViewModel

I'm having difficulty correctly Data Binding my models in the view. I have a MainWindowViewModel which contains a list of AlbumViewModels. Each AlbumViewModel holds an AlbumModel. So I have multiple albums and I need to display information in an ObservableCollection in the AlbumModel. I have two ListBoxes. ListBox1 holds the list of AlbumViewModels that are in my MainWindowViewModel. My second ListBox I want to display the ObservableCollection from the current selected item from the AlbumViewModel.AlbumModel. How can I do this? I've tried binding the DataContext of ListBox2 to the ListBox1 element, along with SelectedItem as the path but that returns 'AlbumViewModel'. Is there anyway to bind ItemsSource of a ListBox to the binding of the DataContext, but in this case binding it to [DataContext].AlbumModel.ObservableCollection or something?
I apologise if it sounds rather complicated!
You can use the fact, that when you bind to a collection, WPF wraps collection to CollectionView. And this guy has CurrentItem.. Bea had good article: How can I sync selection of two data bound ListBoxes? and Dr.WPF is amazing (as usual): ItemsControl: 'C' is for Collection.

Resources