【问题标题】:Exception thrown when Xceed DataGridControl is bound to a DataTable and a sorted column no longer existsXceed DataGridControl 绑定到 DataTable 并且已排序的列不再存在时引发异常
【发布时间】:2013-02-26 23:28:59
【问题描述】:

我正在尝试使用 Xceed (http://wpftoolkit.codeplex.com/) 提供的 Extended WPF Toolkit Community Edition 中提供的优秀 DataGrid。我有一个应用程序,它在一个简单的 DataGridControl 对象中显示报告的结果。用户可以从报告列表中选择报告,并且数据网格使用与报告关联的 DataTable 动态更新。每个 Report 的 DataTable 中的列在名称和数量上都可能不同。使用 WPF 中的默认控件,使用常规 MVVM 数据绑定可以正常工作。这也适用于 Xceed 的 DataGridControl,除非使用列对数据进行排序或分组。

当一列被排序或分组,并且 DataTable 更新为其中没有该列的列时,DataGridControl 会引发 ArgumentException,说明正在排序的列不存在。这是一个例外示例:

System.ArgumentException 未处理
Message='' 类型没有名为 'SAP_MATERIAL_NUMBER' 的属性,因此无法对数据进行排序 收藏。
来源=PresentationFramework StackTrace: 在 System.Windows.Data.BindingListCollectionView.ConvertSortDescriptionCollection(SortDescriptionCollection 排序) 在 System.Windows.Data.BindingListCollectionView.RefreshOverride() 在 System.Windows.Data.CollectionView.Refresh() 在 System.Windows.Data.CollectionView.EndDefer() 在 System.Windows.Data.CollectionView.DeferHelper.Dispose() 在 System.Windows.Controls.ItemCollection.SetCollectionView(CollectionView 看法) 在 System.Windows.Controls.ItemCollection.SetItemsSource(IEnumerable 价值) 在 System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d、DependencyPropertyChangedEventArgs e) ...

这是我当前定义和绑定控件的 XAML:

<xcdg:DataGridControl
    Grid.Row="2"
    AutoCreateColumns="True"
    AutoRemoveColumnsAndDetailConfigurations="True"
    ReadOnly="True"
    x:Name="xceedReportResult"
    ItemsSource="{Binding SelectedReport.Report.Result}"
    FontSize="11">

    <xcdg:DataGridControl.View>
        <xcdg:TableflowView
            ShowRowSelectorPane="False"
            IsAnimatedColumnReorderingEnabled="True"
            HorizontalGridLineBrush="LightGray"
            VerticalGridLineBrush="LightGray"
            IsAlternatingRowStyleEnabled="True"
            ShowScrollTip="False">

            <xcdg:TableflowView.Theme>
                <xcdg:ClassicSystemColorTheme />
            </xcdg:TableflowView.Theme>
        </xcdg:TableflowView>
    </xcdg:DataGridControl.View>

</xcdg:DataGridControl>

...根据 Xceed 论坛的一些建议,我尝试在选择新报告时运行以下代码以清除任何 SortDescriptions 或 GroupDescriptions,但这不起作用:

ICollectionView source = xceedReportResult.ItemsSource as DataGridCollectionView;

if (source != null)
{
    if (source.SortDescriptions != null)
    {
        source.SortDescriptions.Clear();
    }

    if (source.GroupDescriptions != null)
    {
        source.GroupDescriptions.Clear();
    }
}

有没有人以这种方式使用过这个数据网格,并找到了解决这个问题的方法?

【问题讨论】:

    标签: data-binding xceed-datagrid


    【解决方案1】:

    我想我找到了我的问题,或者至少找到了一种在不引发异常的情况下处理此问题的方法。我通过 XAML 代码修改为我的网格使用显式 DataGridCollectionViewSource 声明:

    <Control.Resources>
       <xcdg:DataGridCollectionViewSource
            x:Key="reportResultView"
            x:Name="reportResultView"
            Source="{Binding SelectedReport.Report.Result.DefaultView}"
            AutoCreateItemProperties="True"/>
    </Control.Resources>
    

    然后更新我的 DataGridControl 以将其用作 ItemsSource,而不是直接绑定到 DataTable:

    <xcdg:DataGridControl
        Grid.Row="2"
        AutoCreateColumns="True"
        AutoRemoveColumnsAndDetailConfigurations="True"
        ReadOnly="True"
        x:Name="xceedReportResult"
        ItemsSource="{Binding Source={StaticResource reportResultView}}"
        FontSize="11">
    
        <xcdg:DataGridControl.View>
            <xcdg:TableflowView
                ShowRowSelectorPane="False"
                IsAnimatedColumnReorderingEnabled="True"
                HorizontalGridLineBrush="LightGray"
                VerticalGridLineBrush="LightGray"
                IsAlternatingRowStyleEnabled="True"
                ShowScrollTip="False">
    
                <xcdg:TableflowView.Theme>
                    <xcdg:ClassicSystemColorTheme />
                </xcdg:TableflowView.Theme>
            </xcdg:TableflowView>
        </xcdg:DataGridControl.View>
    
    </xcdg:DataGridControl>
    

    一旦我这样做了,如果已排序或分组的列不存在,它就不再抛出异常,并且数据网格会按预期更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      • 2011-09-10
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      相关资源
      最近更新 更多