This is a migrated thread and some comments may be shown as answers.

[Solved] MVVM and Prism 4 in Silverlight with RIA services

3 Answers 111 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
deepak
Top achievements
Rank 1
deepak asked on 23 Feb 2011, 03:11 PM
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)
        {
            this.DivVisiblity = Visibility.Collapsed;
            _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");
            }
        }

*********************************************************************************************
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).
we are uding 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");

after updating the record.


Please suggest me where i am getting the problem.



3 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 23 Feb 2011, 03:50 PM
Hi deepak,

Can you try calling RadGridView.Rebind() after you submit the changes. Also, binding a collection TwoWay does not make any sense. Instead of creating a new collection every time -- you can create it only once and then when new data arrives you can call Clear() and then add all the items that come from the server. Maybe this will help.

We have another question that we would like you to answer. If you replace RadGridView with a simple ListBox or the MS DataGrid -- is the behavior any different or is it absolutely the same? That is very important for us.

We are looking forward to hearing from you.

Regards,
Ross
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
deepak
Top achievements
Rank 1
answered on 28 Feb 2011, 06:52 AM
Hi Ross,

Thanks for reply.

I have made the RadDatagrid binding two way to one way, and cleared the Context (with context.FamilyMember.clear()). After clearing the context of that paricular collection (FamilyMember),I have again called a callback and get all updated data from database and assign the new value to the context of FamilyMember. Now the context shows me the updated record too (if i put a debug point in my code and in axml page too). But the grid is not binding as it is not calling the get of FamilyMember property

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

.
You have told me to rebind the grid with RadGridView.Rebind()  syntax. But i am using MVVM pattern and bind with the Radgridview in ViewModel class, not in codebehind of  xaml file.

Can you please suggest me how can i access the RadGridview (x:Name="FamilyDetails") from ViewModel.


Thanks
Deepak.

0
Rossen Hristov
Telerik team
answered on 03 Mar 2011, 09:25 AM
Hello deepak,

You should not access UI elements from inside a view model.

We believe that the problem is connected with the Binding of the FamilyMember property, but unfortunately, you did not answer the question we asked in our previous post.

Could you please provide us with the information we have requested?

Greetings,
Ross
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
GridView
Asked by
deepak
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
deepak
Top achievements
Rank 1
Share this question
or