0

当我在 DataGridViewModel 类中调用 FillGPLocationAndPrint 时,我得到了这个异常。下面是内部异常。发生的情况是硬编码的字典被传递到 FillGPLocationAndPrint,它从那里获取数据并将其放入绑定到 DataGrid 的 ItemsSource 属性的 GridData ObservableCollection。这会引发一个名为 CollectionChanged 的​​事件,该事件调用负责增加 DataGrid 高度属性和 MainWindow 高度属性的方法 GridDataChanged。基本上,随着行的增加和减少,网格和窗口高度会动态变化。

编辑:

在进一步研究这个问题后,问题实际上是由

mainVm.WindowHeight += ROW_HEIGHT;

mainVm.WindowHeight -= ROW_HEIGHT;

陈述。出于某种原因,它不喜欢改变窗口的高度。mainVm 是表示主窗口的 ViewModel 的对象,而 WindowHeight 是绑定到主窗口中的 Height 属性的双精度对象。

这是内部异常:

开发人员信息(使用文本可视化工具阅读此内容):引发此异常是因为名称为“grdData”的控件“System.Windows.Controls.DataGrid Items.Count:3”的生成器收到了不同意的 CollectionChanged 事件序列与 Items 集合的当前状态。检测到以下差异:累积计数 2 与实际计数 3 不同。[累积计数为 (Count at last Reset + #Adds - #Removes since last Reset)。] 在索引 1:生成器的项目“{NewItemPlaceholder}”不同来自实际项目“MechQualTestDataEntry.MechTestData”。

以下一个或多个来源可能引发了错误事件: System.Windows.Controls.ItemContainerGenerator System.Windows.Controls.ItemCollection System.Windows.Data.ListCollectionView System.Collections.ObjectModel.ObservableCollection`1[[MechQualTestDataEntry.MechTestData, MechQualTestDataEntry, Version=2.0.5.25, Culture=neutral, PublicKeyToken=null]] (加星标的来源被认为更可能是问题的原因。)

最常见的原因是 (a) 在不引发相应事件的情况下更改集合或其计数,以及 (b) 引发具有不正确索引或项目参数的事件。

异常的堆栈跟踪描述了不一致是如何被检测到的,而不是它们是如何发生的。要获得更及时的异常,请将生成器上的附加属性“PresentationTraceSources.TraceLevel”设置为值“High”并重新运行场景。一种方法是运行类似于以下内容的命令:System.Diagnostics.PresentationTraceSources.SetTraceLevel(myItemsControl.ItemContainerGenerator, System.Diagnostics.PresentationTraceLevel.High) 从即时窗口。这会导致检测逻辑在每个 CollectionChanged 事件之后运行,因此会减慢应用程序的速度。

这是 DataGridViewModel 类(缩小):

    private const int GRID_HEIGHT_MAX = 390;
    private const int GRID_HEIGHT_MIN = 60;
    private const int ROW_HEIGHT = 22;
    private int prevRowCount;
    private double gridHeight;
    private MainWindowViewModel mainVm;
    private ObservableCollection<MechTestData> gridData;
    public DataGridViewModel(MainWindowViewModel mainVm)
    {
        this.mainVm = mainVm;
        GridHeight = GRID_HEIGHT_MIN;
        gridData = new ObservableCollection<MechTestData>();
        gridData.CollectionChanged += GridDataChanged;
    }
    public double GridHeight
    {
        get
        {
            return gridHeight;
        }
        set
        {
            gridHeight = value;
            OnPropertyChanged("GridHeight");
        }
    }
    public ObservableCollection<MechTestData> GridData
    {
        get
        {
            return gridData;
        }
        set
        {
            gridData = value;
            OnPropertyChanged("GridData");
        }
    }
    private void GridDataChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        // If the grid height is less than or equal to
        // the maximum grid height
        if (gridHeight <= GRID_HEIGHT_MAX + 1)
        {
            // If we added rows, increase the grid height
            if (gridData.Count > prevRowCount)
            {
                GridHeight += ROW_HEIGHT;
                mainVm.WindowHeight += ROW_HEIGHT;
            }
        }

        // if the grid height is less than or equal to
        // the minimum grid height
        if (gridHeight > GRID_HEIGHT_MIN)
        {
            // If we deleted rows, decrease the grid height
            if (gridData.Count < prevRowCount)
            {
                GridHeight -= ROW_HEIGHT;
                mainVm.WindowHeight -= ROW_HEIGHT;
            }
        }

        // Cache the row count
        prevRowCount = gridData.Count;
    }
    public void FillGPLocationAndPrint(Dictionary<string, string> gpLocationToDiePrintMap)
    {
        foreach (KeyValuePair<string, string> map in gpLocationToDiePrintMap)
        {
            GridData.Add(new MechTestData(map.Key, map.Value));
        }
    }

ViewModelBase 类:

public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

XAML(减去所有其他组件):

<Window 
x:Class="MechQualTestDataEntry.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:p="clr-namespace:MechQualTestDataEntry.Properties"
xmlns:wpftk="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:local="clr-namespace:MechQualTestDataEntry"
Icon="/MechQualTestDataEntry;component/Resources/MechQualIcon.ico"
Title="MainWindow" 
Background="MidnightBlue"
ResizeMode="CanMinimize"
Height="{Binding WindowHeight}"
MinHeight="{Binding WindowHeight}"
MaxHeight="{Binding WindowHeight}"
MinWidth="700"
MaxWidth="1000"
Width="820">
<Window.Resources>
    <local:DateTimeConverter x:Key="DateTimeFormatter"/>
</Window.Resources>

    <!-- Data Grid -->
    <DataGrid
        DockPanel.Dock="Bottom"
        x:Name="grdData"
        CellStyle="{StaticResource cellStyle}"
        FontFamily="Verdana"
        Height="{Binding GridHeight}"
        Foreground="MidnightBlue"
        AutoGenerateColumns="False"
        RowHeight="22"
        CanUserAddRows="True"
        CanUserDeleteRows="True"
        CanUserReorderColumns="False"
        CanUserResizeColumns="False"
        CanUserResizeRows="False"
        CanUserSortColumns="True"
        ItemsSource="{Binding GridData}"
        SelectionUnit="CellOrRowHeader"
        SelectionMode="Extended">
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridCell}">
                <EventSetter 
                    Event="PreviewMouseLeftButtonDown" 
                    Handler="DataGridCell_PreviewMouseLeftButtonDown"/>
            </Style>
            <Style TargetType="{x:Type DataGridRowHeader}">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="False">
                        <Setter Property="Background" Value="DeepSkyBlue" />
                        <Setter Property="BorderBrush" Value="MidnightBlue" />
                        <Setter Property="BorderThickness" Value="1" />
                        <Setter Property="Width" Value="15" />
                        <Setter Property="HorizontalContentAlignment" Value="Right" />
                        <Setter Property="VerticalContentAlignment" Value="Center" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="MidnightBlue" />
                        <Setter Property="BorderBrush" Value="DeepSkyBlue" />
                        <Setter Property="BorderThickness" Value="1" />
                        <Setter Property="Width" Value="15" />
                        <Setter Property="HorizontalContentAlignment" Value="Right" />
                        <Setter Property="VerticalContentAlignment" Value="Center" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>

            <!-- TO Number -->
            <DataGridTextColumn 
                Width="40"
                Binding="{Binding TONumber}">
                <DataGridTextColumn.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock
                            Style="{StaticResource tbkStyleGridHeader}"
                            TextWrapping="Wrap"
                            Text="TO #"/>
                    </DataTemplate>
                </DataGridTextColumn.HeaderTemplate>
            </DataGridTextColumn>

            <!-- GelPak Location -->
            <DataGridTextColumn 
                Width="60"
                Binding="{Binding GelPakLocation}">
                <DataGridTextColumn.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock
                            Style="{StaticResource tbkStyleGridHeader}"
                            TextWrapping="Wrap"
                            Text="GelPak Location"/>
                    </DataTemplate>
                </DataGridTextColumn.HeaderTemplate>
            </DataGridTextColumn>

            <!-- Die Print -->
            <DataGridTextColumn 
                Width="60"
                Binding="{Binding DiePrint}">
                <DataGridTextColumn.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock
                            Style="{StaticResource tbkStyleGridHeader}"
                            Text="Die Print"/>
                    </DataTemplate>
                </DataGridTextColumn.HeaderTemplate>
            </DataGridTextColumn>

            <!-- Wire Pull Test -->
            <DataGridTextColumn 
                Width="70"
                Binding="{Binding WirePullTestValue}">
                <DataGridTextColumn.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock
                            Style="{StaticResource tbkStyleGridHeader}"
                            TextWrapping="Wrap"
                            Text="Wire Pull Test"/>
                    </DataTemplate>
                </DataGridTextColumn.HeaderTemplate>
            </DataGridTextColumn>

            <!-- Failure Mode -->
            <DataGridTextColumn 
                Width="84"
                Binding="{Binding FailureMode}">
                <DataGridTextColumn.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock
                            Style="{StaticResource tbkStyleGridHeader}"
                            Text="Failure Mode"/>
                    </DataTemplate>
                </DataGridTextColumn.HeaderTemplate>
            </DataGridTextColumn>

            <!-- Ball Shear -->
            <DataGridTextColumn 
                Width="70"
                Binding="{Binding BallShearTestValue}">
                <DataGridTextColumn.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock
                            Style="{StaticResource tbkStyleGridHeader}"
                            TextWrapping="Wrap"
                            Text="Ball Shear Test"/>
                    </DataTemplate>
                </DataGridTextColumn.HeaderTemplate>
            </DataGridTextColumn>

            <!-- Die Shear -->
            <DataGridTextColumn 
                Width="70"
                Binding="{Binding DieShearTestValue}">
                <DataGridTextColumn.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock
                            Style="{StaticResource tbkStyleGridHeader}"
                            TextWrapping="Wrap"
                            Text="Die Shear Test"/>
                    </DataTemplate>
                </DataGridTextColumn.HeaderTemplate>
            </DataGridTextColumn>

            <!-- InP Remaining -->
            <DataGridTextColumn 
                Width="70"
                Binding="{Binding InPRemaining}">
                <DataGridTextColumn.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock
                            Style="{StaticResource tbkStyleGridHeader}"
                            TextWrapping="Wrap"
                            Text="InP Remaining"/>
                    </DataTemplate>
                </DataGridTextColumn.HeaderTemplate>
            </DataGridTextColumn>

            <!-- Test Result -->
            <DataGridTextColumn 
                Width="70"
                Binding="{Binding TestResult}">
                <DataGridTextColumn.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock
                            Style="{StaticResource tbkStyleGridHeader}"
                            TextWrapping="Wrap"
                            Text="Pass/Fail"/>
                    </DataTemplate>
                </DataGridTextColumn.HeaderTemplate>
            </DataGridTextColumn>

            <!-- Notes -->
            <DataGridTextColumn
                Width="*"
                Binding="{Binding Notes}">
                <DataGridTextColumn.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock
                            Style="{StaticResource tbkStyleGridHeader}"
                            TextWrapping="Wrap"
                            Text="Notes"/>
                    </DataTemplate>
                </DataGridTextColumn.HeaderTemplate>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

解决此问题的任何帮助将不胜感激。

问候,

凯尔

4

2 回答 2

1

我的问题通过这种方法解决了。转到 Visual Studio > 工具 > 选项 > 调试 > 数据绑定 > Vorbose。

于 2017-04-17T20:07:24.413 回答
-2

我想我会回答这个问题,因为我发布这个问题已经有一段时间了。问题是 .NET 4.0 安装问题。我们在所有出现此问题的计算机上卸载并重新安装了该框架,并解决了该问题。

于 2013-12-18T15:21:23.133 回答