0

当有 GroupDescriptors 对网格中的项目应用分组时,我的 RadGridView 抛出 System.InvalidCastException。

例外是相当无用的:

[A]DynamicDataType cannot be cast to [B]DynamicDataType. Type A originates from 'DynamicData, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' in a byte array. Type B originates from 'DynamicData, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' in a byte array.

  at Telerik.Windows.Data.FuncExtensions.<>c__DisplayClass1`2.<ToUntypedFunc>b__0(Object item)
   at Telerik.Windows.Data.QueryableCollectionViewGroup.FindLastLevelGroupByItem(Object item)
   at Telerik.Windows.Controls.GridView.GridViewDataControl.GetRowForItem(Object item, Boolean forceGroupExpand)
   at Telerik.Windows.Controls.GridView.GridViewDataControl.get_CurrentCell()
   at Telerik.Windows.Controls.GridView.GridViewDataControl.CanCellBecomeCurrent(GridViewCell cell)
   at Telerik.Windows.Controls.GridView.GridViewCell.OnMouseLeftButtonDown(MouseButtonEventArgs e)
   at System.Windows.Controls.Control.OnMouseLeftButtonDown(Control ctrl, EventArgs e)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

当从网格顶部的栏中删除组时,没有问题,我可以正常选择网格的行。

该问题似乎与我用来在刷新之间保存 GridDescriptors 的一段代码有关。该方法接受 RefreshItemSourceLifetimeStage 这是一个枚举。此代码在 (AboutToRefresh) 之前和之后 (FinishedRefresh) 调用,ItemSource 使用适当的参数进行更改。

    public void RefreshGrid(RefreshItemSourceLifetimeStage stage)
    {
        switch (stage)
        {
            case RefreshItemSourceLifetimeStage.AboutToRefresh:
                if (_grid.ItemsSource == null && !_grid.GroupDescriptors.Any())
                {
                    // default group descriptor that you get on initial page load
                    _savedGroupDescriptors.Add(new GroupDescriptor { Member = "_CallFactorSet.CreatedAt.Year", DisplayContent = "Year Created", SortDirection = ListSortDirection.Descending});
                }
                else
                {
                    _savedGroupDescriptors.Clear();
                    foreach (var igd in _grid.GroupDescriptors)
                    {
                        // we have to clone the objects into the _savedGroupDescriptors collection because otherwise they disappear when the itemsource is refreshed
                        if (igd is GroupDescriptor)
                        {
                            var gd = (GroupDescriptor) igd;
                            _savedGroupDescriptors.Add(new GroupDescriptor { Member = gd.Member, DisplayContent = gd.DisplayContent, SortDirection = gd.SortDirection });
                        }
                        else if (igd is ColumnGroupDescriptor)
                        {
                            var cgd = (ColumnGroupDescriptor) igd;
                            _savedGroupDescriptors.Add(new ColumnGroupDescriptor { Column = cgd.Column, DisplayContent = cgd.DisplayContent, SortDirection = cgd.SortDirection });
                        }
                    }
                }

                break;

            case RefreshItemSourceLifetimeStage.FinishedRefresh:
                _grid.GroupDescriptors.Clear();
                foreach(var groupDescriptor in _savedGroupDescriptors)
                {
                    _grid.GroupDescriptors.Add(groupDescriptor);
                }

                break;

            default:
                throw new ArgumentOutOfRangeException("stage");
        }
    }

有任何想法吗?

4

1 回答 1

0

有趣的是,更新到 Telerik 控件的最新版本(撰写本文时为 2011 年第三季度,高于 2011 年第二季度 SP1)似乎解决了这个问题。

于 2011-11-30T01:42:36.487 回答