Prism 4 and MVVM in Silverlight with RIA services - Gridview Rebind Issue

Topics: Prism v4 - Silverlight 4
Apr 14, 2011 at 11:34 AM
Edited Apr 14, 2011 at 11:35 AM

Hi

I am developing a silverlight  (Version 4) navigation application  with
1. MVVM pattern
2. PRISM 4 framework
3. RIA services
4. Telerik Silverlight Controls (latest version)

I am using following code to update the entity data using Context.

There is a page known a Familay details. In this page i have to show the family members data in RadGridview at the time of load.
I am using the following code.

  1. In VIew:

************************************************************************************
    <telerik:RadGridView x:Name="FamilyDetails" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2"  BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Auto"
                             ScrollViewer.VerticalScrollBarVisibility="Auto" RowIndicatorVisibility="Collapsed" ItemsSource="{Binding FamilyMember, Mode=TwoWay}"  
                             AlternateRowBackground="AliceBlue" AlternationCount="2" AutoGenerateColumns="False" HorizontalAlignment="Left"
                             ShowGroupPanel="False" Margin="10"  Width="940" VerticalAlignment="Top" RowHeight="30" MinHeight="100" >
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="Name" IsResizable="False" IsReadOnly="True" Width="400"  DataMemberBinding="{Binding FamilyMemberName,Mode=TwoWay}" ></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Date of Birth" IsResizable="False" IsReadOnly="True"  Width="200"  DataMemberBinding="{Binding DOB,Mode=TwoWay}" ></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="RelationShip Type" IsResizable="False" IsReadOnly="True" Width="200"  DataMemberBinding="{Binding Description,Mode=TwoWay}" ></telerik:GridViewDataColumn>
                <telerik:GridViewColumn Header="Action" IsResizable="False"  Width="140">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" VerticalAlignment="Top">
                                <telerik:RadButton Width="30" Height="30" HorizontalAlignment="Left" Command="{Binding EditCommand, Source={StaticResource fvVM}}"  CommandParameter="{Binding PatientId}"    Background="Transparent" >
                                    <Image Name="img1" Width="24" Height="24" Source="/OT.Patient;component/Asset/Image/edit.png"></Image>
                                </telerik:RadButton>
                                <telerik:RadButton Width="30" Height="30" HorizontalAlignment="Left" Command="{Binding DeleteCommand, Source={StaticResource fvVM}}"  CommandParameter="{Binding PatientId}" Margin="20,0,0,0" Background="Transparent" >
                                    <Image Name="img2" Width="24" Height="24" Source="/OT.Patient;component/Asset/Image/delete.png"></Image>
                                </telerik:RadButton>
                            </StackPanel>
                            
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewColumn>
               
            </telerik:RadGridView.Columns>

        </telerik:RadGridView>

******************************************************************************************************************
The datacontext for view will set using property in XAML file code behind as
******************************************************************************************************
 [Export(typeof(FamilyDetailView))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public partial class FamilyDetailView : UserControl
    {
        public FamilyDetailView()
        {
            InitializeComponent();
        }
        [Import]
        public FamilyDetailViewModel ViewModel
        {
            get { return this.DataContext as FamilyDetailViewModel; }
            set { this.DataContext = value; }
        }
    
    }


*************************************************************************************************************



In ViewModel
       [ImportingConstructor]
         public FamilyDetailViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;
            PatientDataContext Context = new PatientDataContext();
            var Query = Context.GetVw_GetFamilyMembersQuery();
            Context.Load(Query, LoadBehavior.RefreshCurrent, MyCallBack, null);
}

 private void MyCallBack(LoadOperation lo)
        {
            if (!lo.HasError)
            {
                var entityList = lo.Entities.Where(i => i is vw_GetFamilyMembers).Cast<vw_GetFamilyMembers>().ToList();
                var entity2 = entityList.Where(t => t.ParentPatientId == LoginUser.ID).ToList();
                this.FamilyMember = new ObservableCollection<vw_GetFamilyMembers>(entity2);
                this.OnPropertyChanged("FamilyMember");
            }
        }

 

private ObservableCollection<vw_GetFamilyMembers> _familymember;
 public ObservableCollection<vw_GetFamilyMembers> FamilyMember
            {
              get { return _familymember; }
              set { _familymember = value;  this.OnPropertyChanged("FamilyMember");}
            }


NOTE: The data is retriving from sql2008 server using WCE RIA service.(EDMX file and Domain service class with metadata).

*********************************************************************************************
The above code will successfully bind my grid and display data properly.
But the problem is,  i have to update the grid data after inserting a new data in Entity using datacontext  of Ria service(table). Its updating in database but not rebinding ther grid again.

we are using the follwoing code to update the context.

***********************************************************
context.Patients.Add(this.FamilyPatients);
context.SubmitChanges(); 

***********************************************************

After executing the code the updated data will saved in database (Sql 2008).
But the RadGridview is not showing the updated data.

I will used the

this.OnPropertyChanged("FamilyMember");

 Even after updating the database, i am again calling the domain service to rebind the grid as..

  private void RebingGrid(LoadOperation lo)
        {
            if (!lo.HasError)
            {
                var entityList = lo.Entities.Where(i => i is vw_GetFamilyMembers).Cast<vw_GetFamilyMembers>().ToList();
                var entity2 = entityList.Where(t => t.ParentPatientId == LoginUser.ID).ToList();
                this.FamilyMember = new ObservableCollection<vw_GetFamilyMembers>(entity2);
                this.OnPropertyChanged("FamilyMember");
            }
        }

Now the FamilyMember list will give me all updated record, if i put a breakpoint on it. But the grid (FamilyDetails) is not refreshing/rebinging.

Its not calling the get of FamilyMember property.

Please suggest me where i am getting the problem.

 

Thanks and regards,

Sagar Biswas
sagar@icreon.net

Developer
Apr 14, 2011 at 2:25 PM

Hi Sagar,

One possible reason for this to be happening could be that your ViewModel does not implement the INotifyPropertyChanged interface. Other than that, your code seems to be OK. You could try making your ViewModel inherit from the NotificationObject class, in case it doesn't.

If that doesn't solve your issue, it would be helpful if you could provide us with a repro sample, or more information about your scenario, so that we can help you find the cause of your unexpected behavior.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Aug 29, 2011 at 3:39 AM
Edited Aug 29, 2011 at 6:17 AM

Yes indeed, it would be nice to see a

1. MVVM pattern
2. PRISM 4 framework
3. RIA services
4. Silverlight

Sample so we can all learn from it

maybe the ModularityWithMef sample that ships with Prism, add EF4 Models and Ria Services class to MODULE A, C and F

I have tried but no luck

Regards