Hello Amit,
I apologize that no one has yet repsonded to this post. In case you can still use the help you were looking for, I hope to answer all your questions.
I have attached a sample Silverlight project that loosely mimics the XAML you have attached. It utilizes a ViewModel, sample data generated by Expression Blend, and is written in Silverlight 4 and VB. It illustrates how to bind a ViewModel to a XAML UserControl.
1. Binding to the XamGrid is simple. Since you have data being returned from a WCF service, you need to copy the data to a collection that the XamGrid can be bound to. The convention I prefer is to create a ViewModel class. If you look at the attached sample, the ViewModel constructor gets the data. If you bind the ViewModel to the XAML you can display sample data for design mode and access your service when not in design mode.
Here is a sample of a shared property that returns if the application is in design mode (running in the Blend or Visual Studio Designer):
Public Shared Property InDesignMode() As Boolean Get If Application.Current Is Nothing Or Application.Current.RootVisual Is Nothing Then Return True Else Return DesignerProperties.GetIsInDesignMode( Application.Current.RootVisual) End If End Get Private Set(ByVal value As Boolean) End Set End Property
And here is the code for instantiating the ViewModel, Binding it to the DataContext of the layout and binding the collection in the ViewModel to the XamGrid:
<UserControl.Resources> <local:MainViewModel x:Key="ViewModel"/></UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource ViewModel}}"> <igGrid:XamGrid Name="dgTimeSheet" ItemsSource="{Binding TaskCollection}">
...
2. If you notice in the first sample I posted that the data is not being updated in Project.Name. This is because I did not have all classes implement INotifyPropertyChanged. Attached is the same sample updated to support TwoWay binding. I added a ListBox at the bottom of the app to demonstrate that the underlying data is now updated when changing properties in the XamGrid.
Incidentally, when it comes to binding to an item in the ComboBox, I've found it to be extremely simple and less problematic to utilize an number to serve as an index into the ComboBox. The data source would be a number. You could use a converter to convert a string to the appropriate number.
3. If you use an ObservableCollection (as the sample data does in the attached sample), then if you need to dynamically update the data from the service at run-time, then the records you add or remove will be updated on the UI.
More in the next post...
To complete the set of questions:
4. I've updated the sample one last time to support this question. the best way to update the Total hours is not in the View but in the ViewModel or the Model. The sample data source is notified anytime a value is changed. So it is very easy to update the Total Hours property by simply summing the numbers and assigning them to that property. The sample will do just that.
5. And in like manner, since the sample is "date-driven", when a row is added, the ObservableCollection that is bound to the Grid is automatically updated with the new record. You no longer need to look at any index in the XamGrid itself. A best practice technique is to not deal with record management in the UI but in the data. Silverlight is designed to support the UI presenting and visualizing the data while the engine controls data logic back in the Model or ViewModel (which this sample illustrates).
Thank you!