【问题标题】:WPF DataGrid Gives ArgumentOutOfRangeException when clickedWPF DataGrid 在单击时给出 ArgumentOutOfRangeException
【发布时间】:2015-03-19 21:43:33
【问题描述】:

我对 WPF 和 MVVM 还很陌生,而且总体上是新手,所以提前感谢您的耐心等待。

我在我的模型中使用了一个自定义类,并且我的视图模型中有一个该自定义对象的 ObservableCollection。在类的构造函数中,我在实例化对象时将其添加到集合中。在我看来,我正在使用绑定到集合的 DataGrid 来列出该类的所有活动实例。我正在尝试实现从 DataGrid 拖放到垃圾桶图标上,这将允许用户处理不需要的类实例。

问题是,当您单击 DataGrid 中的任何内容时,程序会立即崩溃,并出现 ArgumentOutOfRange 异常 - ("The given DisplayIndex is out of range. DisplayIndex must be greater or equal to 0 and less than Columns.Count. " "实际值为 0")。 DisplayIndex 似乎与 DataGrid 列有关,所以这个异常可能是因为我没有显示传统意义上的任何列 - 在我的 DataGrid 中,AutoGenerateColumns 设置为 False,并且我正在显示我需要显示的所有内容一个行详细信息模板。 (原因是我显示 DataGrid 的区域很窄,所以我需要一个嵌套的、特定于项目的网格来正确表示项目。)DataGrid 可以很好地显示并与集合同步,但显然有一些问题.我已经阅读了数十个有关 DataGrid 崩溃的链接,但没有发现任何涉及此异常的内容。

我希望的行为是在拖放时将 DataGrid 项表示的自定义对象传递给目标。我不在乎他们单击了哪个“列”或其他任何内容 - 我只需要一种方法将对象引用或 SelectedIndex(集合中的项目索引)传递给视图模型中的方法。

提前感谢您的帮助!有问题的代码 (XAML) 似乎是:

<ScrollViewer DockPanel.Dock="Bottom" Margin="2" Width="180" ScrollViewer.VerticalScrollBarVisibility="Auto">
            <DataGrid ItemsSource="{Binding Path=myCollection, Mode=OneWay}" AutoGenerateColumns="False" RowDetailsVisibilityMode="Visible" HeadersVisibility="None">
                <DataGrid.RowDetailsTemplate>
                    <DataTemplate DataType="model:myClass">
                        <Border CornerRadius="10" Background="AliceBlue">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="50" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding MyString1}" FontSize="21" VerticalAlignment="Bottom" />
                                <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding MyCustomProperty, Converter={StaticResource MyIValueConverter}}" VerticalAlignment="Bottom" />
                                <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding MyString2}" TextWrapping="Wrap" />
                                <Image Source="/Resources/image1.png" Grid.Column="2" Grid.Row="0">
                                    <Image.DataContext>
                                        <Properties:Resources/>
                                    </Image.DataContext>
                                </Image>
                                <Image Source="/Resources/image2.png" Grid.Column="2" Grid.Row="1">
                                    <Image.DataContext>
                                        <Properties:Resources/>
                                    </Image.DataContext>
                                </Image>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </DataGrid.RowDetailsTemplate>
            </DataGrid>
        </ScrollViewer>

【问题讨论】:

    标签: c# wpf xaml mvvm datagrid


    【解决方案1】:

    问题确实是因为我没有生成任何“传统”列。这显然是一个已知的错误,记录在这里:Microsoft Bug Report

    作为一种解决方法,我刚刚在 DataGrid 中定义了一个不可见的列,它现在似乎表现正常:

    <DataGrid.Columns>
         <DataGridTemplateColumn Visibility="Hidden" />
    </DataGrid.Columns>
    

    【讨论】:

    • 修复了我的问题。错误报告的链接已损坏,但我使用的是 .Net 4.6.1
    猜你喜欢
    • 2011-07-11
    • 1970-01-01
    • 2012-11-22
    • 2014-10-09
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 2013-10-14
    • 2012-07-25
    相关资源
    最近更新 更多