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

Binding to a property which is not in baseclass

12 Answers 204 Views
GridView
This is a migrated thread and some comments may be shown as answers.
NoRyb
Top achievements
Rank 1
NoRyb asked on 24 Mar 2010, 08:30 AM
Hello

A few days ago I had the problem, that I was binding a GridViewDataColumn to a Property that does not necessary exist on the type of the associated DataContext but in subclasses it does. Here a little overview of my support ticket post:

I'm trying to replace my DataGrid with a RadGridView from Telerik. My control has a ViewModel as DataContext which has a property "DbObject". It is of the type "DbBaseObject". Now there could be any descendant of this type filled into the GridView. Some of them may have a property "Name", some may not. The standard WPF-Controls are pretty soft when evaluating a property that doesn't exist -> they just show an empty value. The RadGridView gives me Exceptions: 
 
ArgumentException "Property with name Name cannot be found on type DbBaseObject" 
and afterwards 
Invalid property or field - 'Name' for type: DbBaseObject 
 
This is true, the DbBaseObject doesn't have a property "Name" but this shouldn't be a reason not to show a column. Is there anything I can do to soften this behavior up? 

The answer was, that somehow it is not possible with the Telerik GridView to bind to abstract classes and that I have to use a Converter to achieve this job. I did so and it worked although I was not 100% happy with this solution as in my opinion, databinding should be a lot softer. Now I'm confronted with the problem, that Grouping / Filtering does not really work as it gives me Exceptions - after searching this forum I found the following post that answers why it doesn't work:


Any other idea how I achieve what I want to achieve? (I was not able to answer the support ticket as my 60-Days-Support expired) 

Thanks
~NoRyb

12 Answers, 1 is accepted

Sort by
0
Stefan Dobrev
Telerik team
answered on 26 Mar 2010, 12:27 PM
Hi NoRyb,

We throw this exceptions, but they are internally catch by RadGridView, so the only way to see them is if you have run your application in debug mode and have break on exception turned on.

Regarding the filtering and grouping it is supposed to not work when your objects does not have a specific property. It will be impossible for RadGridView to filter or group items which does not have the specified property by which you want to group or filter.

Sincerely yours,
Stefan Dobrev
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
NoRyb
Top achievements
Rank 1
answered on 29 Mar 2010, 08:17 AM
Hello Stefan

Okay, the Exceptions on the GridView are caught internally, I get that. And that it didn't work with converters is logical too - so I built it back and turned off the exceptions. Now building up the GridView works. The problem now is, as soon as I try to group the GridView (by dragging and dropping a column) I get the following exception:

System.Reflection.TargetInvocationException: 
   bei System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) 
   bei System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) 
   bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) 
   bei System.Delegate.DynamicInvokeImpl(Object[] args) 
   bei System.Windows.RoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 
   bei System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 
   bei System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
   bei System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 
   bei System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) 
   bei System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) 
   bei Telerik.Windows.Controls.GridView.GridViewDataControl.RaiseGroupingEvent(IColumnInfo draggedCell, Int32 index, GroupingEventAction groupingEventAction) 
   bei Telerik.Windows.Controls.GridView.GridViewGroupPanel.OnDropInfo(Object sender, DragDropEventArgs e) 
   bei Telerik.Windows.Controls.DragDrop.DragDropEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 
   ... 

interesting is, that the inner exception is again:
System.ArgumentException: 
   bei Telerik.Windows.Data.Expressions.MemberAccessTokenExtensions.CreateMemberAccessExpression(IMemberAccessToken token, Expression instance) 
   bei Telerik.Windows.Data.Expressions.ExpressionFactory.MakeMemberAccess(Expression instance, String memberName) 
   bei Telerik.Windows.Data.Expressions.ExpressionFactory.MakeMemberAccess(Expression instance, String memberName, Boolean liftMemberAccessToNull) 
   bei Telerik.Windows.Data.Expressions.PropertyAccessExpressionBuilder.CreateMemberAccessExpressionOverride() 
   bei Telerik.Windows.Data.Expressions.MemberAccessExpressionBuilderBase.CreateMemberAccessExpression() 
   bei Telerik.Windows.Data.GroupDescriptor.CreateGroupKeyExpression(ParameterExpression parameterExpression) 
   bei Telerik.Windows.Data.GroupDescriptorBase.CreateGroupKeyExpression(Expression itemExpression) 
   bei Telerik.Windows.Data.Expressions.GroupDescriptorExpressionBuilder.CreateGroupKeySelectorExpression() 
   bei Telerik.Windows.Data.Expressions.GroupDescriptorExpressionBuilder.get_GroupKeySelectorExpression() 
   bei Telerik.Windows.Data.Expressions.GroupDescriptorExpressionBuilderBase.CreateQuery() 
   bei Telerik.Windows.Data.Expressions.GroupDescriptorCollectionExpressionBuilder.CreateQuery() 
   bei Telerik.Windows.Data.QueryableExtensions.GroupBy(IQueryable source, IEnumerable`1 groupDescriptors) 
   ... 

Message:
"Invalid property or field - 'Name' for type: DbBaseObject"

And this time I have no Exceptions turned on and it freezes my application when running a release outside of the debugging environment. Any suggestion how I get over that?

Kind regards
0
Stefan Dobrev
Telerik team
answered on 30 Mar 2010, 12:58 PM
Hello NoRyb,

As I have mentioned previously RadGridView will not be able to group by properties that are not present on your objects. What you can do is implement IComparable / IEquatable<T> on your objects and compare them by Name. Then in the column which is displaying the name you have to bind to the whole business object (using binding path=.). This way when you group by that column it will execute the grouping via your interface implementation.

Greetings,
Stefan Dobrev
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
NoRyb
Top achievements
Rank 1
answered on 01 Apr 2010, 02:08 PM
Hello Stefan

Well the properties are present on the objects, just not in the declared base-class. But is there a way to use IComparable or any other Interface with my ItemViewModels? Kind of a Callback which tells me to compare 2 rows and which column was filtered / grouped so I can decide what happens? I'm thinking of something like in WinForms where you had the ItemSorter-Object which you could set for a ListView.

I also tried to go back to Converters and implemented IComparable and IEquatable on my GridItemViewModel and Bound the Rows to ".". This didn't work, neither "Equals" nor "CompareTo" was ever called [Edit: And I get a TargetInvocationException].

Thank you
0
Stefan Dobrev
Telerik team
answered on 07 Apr 2010, 07:15 AM
Hello NoRyb,

If you want to implement custom sorting you can use the Sorting event of RadGridView. In its arguments you can check the column that is sorted by and provide your own sorting logic by canceling the event and assigning a new ItemsSource that is your original collection sorted by your own custom logic.

However my suggestion is to create view models for you items that you are displaying in the grid. Here is a blog post which illustrates how to create view models for collection of items. If you use this approach you will get sorting out of the box.

Greetings,
Stefan Dobrev
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
NoRyb
Top achievements
Rank 1
answered on 09 Apr 2010, 07:26 AM
Hello Stefan

I'm really sorry to still bother you with this. I do have ViewModels for my items and I implemented the IComparable-Interface. Now when I sort, it jumps into the CompareTo-Method of each object but I have no information on which column was clicked so I don't really know which property to sort (same with Filtering). Secondly, the Grouping still does not work (still gives this exception) and I really need a solution for all of these three functions.

Thank you
0
Stefan Dobrev
Telerik team
answered on 12 Apr 2010, 11:30 AM
Hello NoRyb,

Can you open a support ticket and attached your project there? We will modify it and send you back the updated version. Will that work for you?

Greetings,
Stefan Dobrev
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
NoRyb
Top achievements
Rank 1
answered on 12 Apr 2010, 12:37 PM
Hello Stefan

Thanks for trying but I'm not allowed to send you the project and it's pretty difficult to get just that part out for you. Second thing is, that I'm not allowed to open support tickets as my 60-Days-Direct support is expired. If you can code a sample solution I'm sure I will understand. The key points are:

- My ItemViewModels have a Property which holds an object of a type. This type is a baseclass and does not yet have all the attributes that I bind the columns to. The objects that are filled into the GridView may have the bound attribute or not as they extend the baseclass.
- I would like to be able to Filter, Group and Sort the GridView. Of course if one of the objects does not have an attribute, a null value should be taken or this specific object should just not be grouped/filtered/sorted but no exception should occur.
- I'm using MVVM and I have ItemViewModels for the items.

Regards
NoRyb
0
NoRyb
Top achievements
Rank 1
answered on 16 Apr 2010, 02:07 PM
Have you had any chance to look into it yet?
0
Stefan Dobrev
Telerik team
answered on 22 Apr 2010, 11:09 AM
Hi NoRyb,

Currently you cannot execute any data operation in such scenario. The only way to implement this is to introduce view models for your each of your items that the grid is bound to. This view model will have all the properties you want to display in the grid. For the items that does not have such a property you will return null in the view model. This way you will create a view model wrapper around each of your objects that the grid is bound to.

All the best,
Stefan Dobrev
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
NoRyb
Top achievements
Rank 1
answered on 27 Apr 2010, 09:49 AM
Hi

Okay, then I must accept this. Is it possible that in near future this will be working or is it such an exotic problem that you will not bother about it?

Kind Regards
0
Stefan Dobrev
Telerik team
answered on 28 Apr 2010, 08:54 AM
Hi NoRyb,

We will definitely look into ways to enable your scenario as well in the future. I cannot commit to any specific date when this will be available, but you can expect it in post Q2 2010 timeframe.

Regards,
Stefan Dobrev
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
NoRyb
Top achievements
Rank 1
Answers by
Stefan Dobrev
Telerik team
NoRyb
Top achievements
Rank 1
Share this question
or