Selecteditem event in MVVM silverlight - 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?

Related

WPF window inserting an array into DataGrid

WPF window in C# doesn't have option of Inserting something into DataGrid immediately, like as WinForms Form
DataGridView.Rows.Add(whatever)
What is alternative for this code 👆?
So how can I insert an array into DataGridView in WPF window?
You can actually add objects directly to the Items property of the DataGrid:
dataGrid.Items.Add(whatever);
But if you want to be able to edit the items, you should set or bind the ItemsSource property to an IList:
dataGrid.ItemsSource = new List<object> { whatever };
You need to bind the DataGrid to an ObservableCollection. If you insert into the ObservableCollection, it'll appear on the UI

"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.

Master View implementation using MVVM in 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.

How do I loop through a set of items in a databound Silverlight control such as the ListBox?

I have a silverlight ListBox that has it's ItemsSource set. From the C# code behind I want to loop through each ListBox item and change the value of the text and access controls that are in the ListBox ItemTemplate. How would I do that?
ListBox controls in silverlight are bound to an ienumerable type, so that if any value in the ListBox changes, the underlying data is changed and vice versa depending on what type of binding you require. To effectively iterate the items you'll want to iterate through the enumerable object you've bound to.
You can get to the collection by accessing the ListBox.ItemsSource property and change the text of appropriate items, perform LINQqueries etc. If you have bound the controls correctly, saving the collection should update the list.
Hope this helps!

How to bind a custom value to the Text property of a TextBlock inside of a ListBox DataTemplate?

I would like my ListBox to number each ListItem using its index + 1.
How would I do that to the Text property of a TextBlock in a DataTemplate of the ListBox?
If each ListBoxItem uses SelectedIndex + 1, they will all display the same value since SelectedIndex is a scalar. Moreover, this number will change as the user selects different ListBoxItems. I suspect you actually want to display each item's index within the ListBox + 1.
To achieve this, you're probably best off using the ListBox's ItemsContainerGenerator to get the index of the item within the container (see the IndexFromContainer method). You could look at exposing this from your data class, or perhaps look into an attached readonly property that retrieves this value for you.
I had the same question. So far I'm just using my data model to provide the numbers...

Resources