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

Binding ItemsSource to ObservableCollection<BaseClass> causes exception

5 Answers 201 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.
Evan
Top achievements
Rank 1
Evan asked on 24 Sep 2010, 09:59 PM
I have a RadGridView set up to handle displaying a number of reports, and the user story says that I need to get the configuration information for each of them into the detail view, but the configuration information is different for each one (potentially). So I created a base class that contains all the common information, and then concrete classes for the specific implementations, I load an observable collection of the base class with the concrete classes and assign that to the ItemsSource and i get an exception telling me that it can't cast from one concrete class to another (second concrete class type in list to first concrete class type in list).

Does the ItemsSource not recognize the base class i'm using in my observable collection? Any suggestions for how to work around this? The RowLoaded event is handling the loading of the data templates for the different class types (if I can get them loaded into the grid).

I have tried to include all the relevant information for perusal:

RadGridView Version:

2009.3.1208.1030


Property:

private ObservableCollection<ReportDefinitionVMBase> activeReports;
public ObservableCollection<ReportDefinitionVMBase> ActiveReports
{
    get
    {
        return this.activeReports;
    }
    set
    {
        this.activeReports = value;
        PropertyChangedHandler("ActiveReports");
    }
}


Binding:

<grid:RadGridView Name="reportGrid"
                  Grid.Row="2"
                  Opacity="1"
                  ColumnsWidthMode="Fill"
                  CanUserResizeColumns="False"
                  VerticalAlignment="Top"
                  AutoGenerateColumns="False"
                  IsReadOnly="False"
                  RowIndicatorVisibility="Collapsed"
                  CanUserFreezeColumns="False"
                  UseAlternateRowStyle="True"
                  ColumnBackground="{x:Null}"
                  ShowGroupPanel="False"
                  CanUserReorderColumns="False"
                  IsFilteringAllowed="False"
                  RowDetailsVisibilityMode="VisibleWhenSelected"
                  Background="Transparent"
                  Width="Auto"
                  Height="Auto"
                  ItemsSource="{Binding ActiveReports}"
                  Style="{StaticResource RadGridViewSettings}"
                  RowLoaded="ReportRowLoadedHandler"
                  >


Exception:

System.InvalidCastException was unhandled by user code
  Message="Unable to cast object of type 'Ceridian.Hrpr.Sl.Payroll.ReportOptions.PayrollSummaryVM' to type 'Ceridian.Hrpr.Sl.Payroll.ReportOptions.RegisterVM'."
  StackTrace:
       at System.Linq.Enumerable.<CastIterator>d__aa`1.MoveNext()
       at Telerik.Windows.Data.QueryableCollectionView.PopulateInternalList(IQueryable view)
       at Telerik.Windows.Data.QueryableCollectionView.CreateInternalList()
       at Telerik.Windows.Data.QueryableCollectionView.EnsureInternalList()
       at Telerik.Windows.Data.QueryableCollectionView.get_InternalList()
       at Telerik.Windows.Data.QueryableCollectionView.get_IsEmpty()
       at Telerik.Windows.Controls.GridView.BaseItemsControl.OnItemCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
       at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
       at Telerik.Windows.Data.DataItemCollection.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
       at Telerik.Windows.Data.DataItemCollection.SetItemsSource(IEnumerable source)
       at Telerik.Windows.Controls.GridView.GridViewDataControl.<>c__DisplayClass47.<Bind>b__46()
       at Telerik.Windows.Controls.CursorManager.PerformTimeConsumingOperation(FrameworkElement frameworkElement, Action action)
       at Telerik.Windows.Controls.GridView.GridViewDataControl.Bind(Object newValue)
       at Telerik.Windows.Controls.GridView.GridViewDataControl.OnItemsSourceChanged(Object oldValue, Object newValue)
       at Telerik.Windows.Controls.DataControl.OnItemsSourcePropertyChanged(DependencyObject origin, DependencyPropertyChangedEventArgs args)
       at Telerik.Windows.PropertyMetadata.<>c__DisplayClass1.<Create>b__0(DependencyObject d, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.RefreshExpression(DependencyProperty dp)
       at System.Windows.Data.BindingExpression.RefreshExpression()
       at System.Windows.Data.BindingExpression.SendDataToTarget()
       at System.Windows.Data.BindingExpression.SourcePropertyChanged(PropertyPathListener sender, PropertyPathChangedEventArgs args)
       at System.Windows.PropertyPathListener.RaisePropertyPathStepChanged(PropertyPathStep source)
       at System.Windows.PropertyAccessPathStep.RaisePropertyPathStepChanged(PropertyListener source)
       at System.Windows.CLRPropertyListener.SourcePropertyChanged(Object sender, PropertyChangedEventArgs args)
       at System.Windows.Data.WeakPropertyChangedListener.PropertyChangedCallback(Object sender, PropertyChangedEventArgs args)
       at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)
       at Ceridian.Core.Sl.Infrastructure.ViewModelBase.PropertyChangedHandler(String propertyName, Boolean setDirtyBit)
       at Ceridian.Core.Sl.Infrastructure.ViewModelBase.PropertyChangedHandler(String propertyName)
       at Ceridian.Hrpr.Sl.Payroll.ReportOptions.ReportOptionsVM.set_ActiveReports(ObservableCollection`1 value)
       at Ceridian.Hrpr.Sl.Payroll.ReportOptions.ReportOptionsVM.LoadActiveReportDetails(ObservableCollection`1 detailList)
       at Ceridian.Hrpr.Sl.Payroll.ReportOptions.ReportOptionsVM.<LoadReports>b__0(Object o, GetReportingOptionsSummaryDetailsCompletedEventArgs e)
       at Ceridian.Core.Sl.Infrastructure.ViewModelBase.<>c__DisplayClass6`1.<CallService>b__4(Event`1 p)
       at System.Collections.Generic.AnonymousObserver`1.Next(T value)
       at System.Collections.Generic.AbstractObserver`1.OnNext(T value)
       at System.Collections.Generic.AnonymousObservable`1.AutoDetachObserver.Next(T value)
       at System.Collections.Generic.AbstractObserver`1.OnNext(T value)
       at System.Linq.Observable.<>c__DisplayClasse2`1.<>c__DisplayClasse4.<FromEvent>b__df(Object sender, TEventArgs eventArgs)
       at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
       at HrprPayrollDomainServiceClient.OnGetReportingOptionsSummaryDetailsCompleted(Object state)
  InnerException:




5 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 27 Sep 2010, 07:05 AM
Hello Evan,

 This version is our Q3 2009 Sp1 - can you try more recent version instead?

All the best,
Vlad
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Evan
Top achievements
Rank 1
answered on 27 Sep 2010, 03:05 PM
Vlad,

Unfortunately upgrading the controls at this point is not an option, I need to be able to make it work in the current version of the control.

Regards,

Evan
0
Vlad
Telerik team
answered on 30 Sep 2010, 11:44 AM
Hi Evan,

 Can you try to upgrade at least to Q3 2009 SP2?

Greetings,
Vlad
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Evan
Top achievements
Rank 1
answered on 30 Sep 2010, 03:00 PM
Vlad,

rather than try the upgrade path (which I don't have control over) I have done a workaround: I've combined all of the properties that can exist between all of the different types of option screens and I'm switching out the template using the RowLoaded event and examining the properties on the DataItem.

If you can guarantee me that this will work differently in an upgraded version I can put that information forward for the developers on the "core" team to consider, but in an enterprise environment, with so many different teams and responsibilities, etc. I can't push an upgrade to a control or toolkit.

Regards,

Evan
0
Vlad
Telerik team
answered on 30 Sep 2010, 03:05 PM
Hi Evan,

 If you can send us small application where we can replicate the exception we can test it against our latest version.

Sincerely yours,
Vlad
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Evan
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Evan
Top achievements
Rank 1
Share this question
or