PRISM RaisePropertyChanged

Topics: Prism v4 - WPF 4
Apr 10, 2012 at 6:52 AM

Hi
I'm using prism and MVVM architecture for my WPF application and I have a problem with RaisePropertyChanged,I have a gridview in my view that its itemsource is binded to an ObservableCollection named AllOrderProducts in my viewmodel,the problem is that when I change AllOrderProducts and i use this.RaisePropertyChanged(() => this.AllOrderProducts);
in doesn't refresh the gridview in my view, and I have this problem with other fields or properties in my viewmodel when I use RaisePropertyChanged
 
can anyone help me with this problem plzz????

Apr 10, 2012 at 11:59 AM

I have a couple of suggestions/questions:

- when you set a breakpoint on the RaisePropertyChanged, is this getting hit

- when running your app in F5 mode, do you see any BindingExpression errors in the Output window?

If you are still having problems, it would help if you post some XAML and viewmodel code to reproduce the problem. In principle, what you are trying to do should work.

Also, I believe that using an ObservableCollection should mean that you don't need to use a raisepropertychanged event. Perhaps someone can correct me if I'm wrong on that one.


Apr 10, 2012 at 4:53 PM

thank you for replying...

about our first question answer in yes the breakpoint gets hit,and I have no error while I'm running the application.

I have two textboxes in my xaml:

<TextBox Text="{Binding Path=Rrgo02,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding CurrentOrder}" />        

<TextBox Text="{Binding Path=Rrgo04,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding CurrentOrder}" />

as you see they are binded to CurrentOrder that is defined like this

public RRegisterOrder CurrentOrder      

{            get { return _currentOrder; }   

        private set           

{               

  _currentOrder = null;               

  _currentOrder = value;               

  RaisePropertyChanged("CurrentOrder");           

  }       

}

and a button :

 <Button Command="{Binding NextCommand}" Content="Next" />

and in my viewmodel:

in the execute part of my NextCommand i call amethod like this and it works:

public void Next(object parameter)       

{           

var tmp = from o in _orEntities.RRegisterOrder.Where(o => o.Rrgo36 == -1 && o.Rrgo01 > CurrentOrder.Rrgo01).OrderBy(o => o.Rrgo01).Take(1)                      select o;           

if (tmp.Count() > 0)               

this.CurrentOrder = tmp.Single();           

GetAllOrderProducts();  

}

 

and i have a gridview :

<telerik:RadGridView CanUserInsertRows="True"  ItemsSource="{BindingPath=AllOrderProducts}"  x:Name="RadGridView1" ShowGroupPanel="False" >   

 <telerik:RadGridView.Columns>               

     <telerik:GridViewDataColumn DataType="{x:Null}" Header="ردیف" UniqueName="Rorp01" />               

     <telerik:GridViewDataColumn DataType="{x:Null}" Header="کد انبار" UniqueName="Rorp02" />           

</telerik:RadGridView.Columns>       

</telerik:RadGridView>

 

and in my viewmodel:

        public ObservableCollection<ROrderProducts> AllOrderProducts       {            get;           private set;        }

and in GetAllOrderProducts() method:

public void GetAllOrderProducts()       

{           

var tmp = from o in _orEntities.ROrderProducts.Where(o => o.Rorp01 == CurrentOrder.Rrgo01 && o.Rorp11 == CurrentOrder.Rrgo36)                     

select o;           

this.AllOrderProducts = new ObservableCollection<ROrderProducts>(tmp.ToList());            

 }

 

when this method is called AllOrderProducts updates and it will be set to corrcect value but the data shown in my gridview is still the same and its not updated...

Here is the problem......!!!!

Developer
Apr 10, 2012 at 9:10 PM

Hi,

In my opinion, the problem you are experiencing might be related to the following line of the code snippets you posted:

// In the GetAllOrderProducts method

this.AllOrderProducts = new ObservableCollection<ROrderProducts>(tmp.ToList());  

According to the description of the ObservableCollection class, the collection notifies a change when an item is added, removed or when the collection is refreshed. However, in the aforementioned line of code, none of these operations is performed; instead, the whole collection is replaced with a new one. This mean that the collection is not being modified; the one modified is the property that references the collection.

Based on my understanding, a possible approach could be to invoke the RaisePropertyChanged method right after the aforementioned line.

If this is not the case, it would be helpful if you could provide us with a repro sample application portraying this problem so that we can help you further with this.

Regards,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Apr 11, 2012 at 6:17 AM

thank you for replying...

I've tried invoking RaisePropertyChanged  but it didn't work...isn't there any other sulotion to solve this problem...if not how can i send you my application??

Developer
Apr 11, 2012 at 2:06 PM

Hi,

You could upload your repro sample application to SkyDrive, or any other storage site you'd like to, and post the link here.

Regards,

Agustin Adami
http://blogs.southworks.net/aadami


Apr 11, 2012 at 8:57 PM

here is the link to my project:

https://skydrive.live.com/redir.aspx?cid=f9298de8d2bd14a7&resid=F9298DE8D2BD14A7!107&parid=F9298DE8D2BD14A7!106&authkey=!ABJyBHE-9MUWp6w

 

hope you can help me...

Developer
Apr 12, 2012 at 9:42 PM

Hi,

After we could mock the database and make the sample to run correctly, we found that the error you are experiencing is being caused by the fact that your OrderView and OrderDetailView (the one that host the RadDataGrid) are using two different instances of the same view model.

The reason behind this is that the OrderViewModel is being exported with the attribute [PartCreationPolicy(CreationPolicy.NonShared)]. This means that each time this type is imported a different instance of the view model will be created. As both the OrderView and the OrderDetailView declare an import of type OrderViewModel in their code behind, each of the views will obtain a different instance of the view model.

When you invoked the RaisePropertyChange method in the view model of the OrderView, the view was notified of the changes correctly. But as the OrderDetailView had a different view model, no changes in the data were performed and no notifications were raised to this view.

If you wish to share the same view model between the two views, a possible approach could be to define the OrderViewModel as a singleton by changing the aforementioned attribute for this one: [PartCreationPolicy(CreationPolicy.Shared)]. Another possible approach is to eliminate the ViewModel property and its Import attribute from the OrderDetailView's code behind. The view then will inherit the view model of the OrderView. As a side note we have checked that both approaches seem to work correctly.

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini

 

[PartCreationPolicy(CreationPolicy.NonShared)]
Apr 12, 2012 at 9:50 PM

It worked....

thank you so much dear Damian..

that was a big help!