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

Issue with RadGridView and DynamicObject

2 Answers 91 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 01 Feb 2012, 07:39 PM
Hi,

I'm using WPF controls 2011.3.1220.40, and using a MVVM pattern with a ViewModel that uses DynamicObject to expose Model properties to the View.

Displaying dynamic property data in the gridview works fine, grouping on one of the real ViewModel properties works fine.  But when the user tries to group by one of the dynamic property columns, a null reference exception is thrown (stack trace below).  After the user clicks the grouping icon, TryGetMember is never called.

I've created a small project to demonstrate the issue, but could not upload a zip file.  So below I've pasted a .xaml and it's codebehind.   To see the problem, try grouping on the "One" column (works), and the "Two" column (exception).

I'm really hoping Telerik folks can look into this issue asap.

Thanks

MainWindow.xaml:
<Window
        x:Class="TestingGridViewControl.MainWindow"
        DataContext='{Binding RelativeSource={RelativeSource Mode=Self}}'
        >
    <Grid>
        <telerik:RadGridView ItemsSource='{Binding Items}' AutoGenerateColumns='False'
            EnableColumnVirtualization='True' IsReadOnly='True' >
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header='One' DataMemberBinding='{Binding One}' />
                <telerik:GridViewDataColumn Header='Two' DataMemberBinding='{Binding Two}' />
                <telerik:GridViewDataColumn Header='Three' DataMemberBinding='{Binding Three}' />
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
 
    </Grid>
     
</Window>

MainWindow.xaml.cs:
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Reflection;
using System.Windows;
 
namespace TestingGridViewControl
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            SetData();
        }
 
        private void SetData()
        {
            Items = new ObservableCollection<ViewModel>
                        {
                            new ViewModel(new Model { Two = "2",  Three = "3",  }) {One = "1"},
                            new ViewModel(new Model { Two = "22", Three = "33", }) {One = "11"},
                        };
        }
 
        public ObservableCollection<ViewModel> Items { get; set; }
    }
 
    public class ViewModel : DynamicObject
    {
        private Model _model;
 
        public ViewModel(Model model)
        {
            _model = model;
        }
 
        public string One { get; set; }
 
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            MethodInfo getMethod;
 
            // If VM has this property, VM takes precedence. 
            // Otherwise, check for property in the Model.
            var vmPropInfo = GetType().GetProperty(binder.Name);
            if (vmPropInfo != null && vmPropInfo.CanRead)
            {
                getMethod = vmPropInfo.GetGetMethod();
                result = getMethod.Invoke(this, null);
                return true;
            }
 
            var modelPropInfo = typeof(Model).GetProperty(binder.Name);
            if (modelPropInfo != null && modelPropInfo.CanRead)
            {
                getMethod = modelPropInfo.GetGetMethod();
 
                result = getMethod.Invoke(_model, null);
                return true;
            }
 
            result = null;
            return false;
 
        }
 
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            MethodInfo setMethod;
 
            var vmPropInfo = GetType().GetProperty(binder.Name);
            if (vmPropInfo != null && vmPropInfo.CanWrite)
            {
                setMethod = vmPropInfo.GetSetMethod();
                setMethod.Invoke(this, new[] { value });
                return true;
            }
 
            var modelPropInfo = typeof(Model).GetProperty(binder.Name);
            if (modelPropInfo != null && modelPropInfo.CanRead)
            {
                setMethod = modelPropInfo.GetSetMethod();
                setMethod.Invoke(_model, new[] { value });
                return true;
            }
 
            return false;
        }
    }
 
    public class Model
    {
        public string Two { get; set; }
        public string Three { get; set; }
    }
}




----- Main Exception -----
Class: System.NullReferenceException
Message: Object reference not set to an instance of an object.
StackTrace:
   at Telerik.Windows.Controls.GridView.FilterEditorFactory.CreateEditor(Type type) in c:\TB\117\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Filtering\FilterEditorFactory.cs:line 55
   at Telerik.Windows.Controls.GridViewBoundColumnBase.CreateFieldFilterEditor() in c:\TB\117\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Columns\GridViewBoundColumnBase.Filtering.cs:line 106
   at Telerik.Windows.Controls.GridView.FilteringControl.CreateFieldFilterEditor(GridViewBoundColumnBase column) in c:\TB\117\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Filtering\FilteringControl.cs:line 268
   at Telerik.Windows.Controls.GridView.FilteringControl.PrepareFieldFilters(GridViewBoundColumnBase column) in c:\TB\117\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Filtering\FilteringControl.cs:line 256
   at Telerik.Windows.Controls.GridView.FilteringControl.Prepare(GridViewBoundColumnBase column) in c:\TB\117\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Filtering\FilteringControl.cs:line 247
   at Telerik.Windows.Controls.GridView.FilteringDropDown.PrepareFilteringControl() in c:\TB\117\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Filtering\FilteringDropDown.cs:line 266
   at Telerik.Windows.Controls.GridView.FilteringDropDown.OnDropDownPopupOpened(Object sender, EventArgs e) in c:\TB\117\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Filtering\FilteringDropDown.cs:line 365
   at System.Windows.FrameworkElement.RaiseClrEvent(EventPrivateKey key, EventArgs args)
   at System.Windows.Controls.Primitives.Popup.OnOpened(EventArgs e)
   at System.Windows.Controls.Primitives.Popup.CreateWindow(Boolean asyncCall)
   at System.Windows.Controls.Primitives.Popup.OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at System.Windows.Controls.Primitives.Popup.set_IsOpen(Boolean value)
   at Telerik.Windows.Controls.GridView.FilteringDropDown.OnIsDropDownOpenChanged(Boolean oldValue, Boolean newValue) in c:\TB\117\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Filtering\FilteringDropDown.cs:line 129
   at Telerik.Windows.Controls.GridView.FilteringDropDown.OnIsDropDownOpenChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) in c:\TB\117\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Filtering\FilteringDropDown.cs:line 75
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at Telerik.Windows.Controls.GridView.FilteringDropDown.set_IsDropDownOpen(Boolean value) in c:\TB\117\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Filtering\FilteringDropDown.cs:line 64
   at Telerik.Windows.Controls.GridView.FilteringDropDown.OnDropDownButtonClick(Object sender, RoutedEventArgs e) in c:\TB\117\WPF_Scrum\Release_WPF\Sources\Development\Controls\GridView\GridView\GridView\Filtering\FilteringDropDown.cs:line 378
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
   at System.Windows.Controls.Primitives.ButtonBase.OnClick()
   at System.Windows.Controls.Primitives.ToggleButton.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

2 Answers, 1 is accepted

Sort by
0
Yordanka
Telerik team
answered on 02 Feb 2012, 11:18 AM
Hello Eric,

Please open a support ticket and attach your project there. We will check what is going wrong and will provide you with further assistance.
 
Kind regards,
Yordanka
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Eric
Top achievements
Rank 1
answered on 02 Feb 2012, 05:51 PM
Done, thanks.   I was so intent on characterizing the bug, I didn't even see the links for support tickets  :-)
Tags
GridView
Asked by
Eric
Top achievements
Rank 1
Answers by
Yordanka
Telerik team
Eric
Top achievements
Rank 1
Share this question
or