WPF DataGrid ArgumentOutOfRangeException(VisualCollection)

时间:2014-11-18 21:47:34

标签: c# wpf datagrid wpfdatagrid visualization

我的DataGrids ConnectionManageForm中有两个dgBinaryOutputs名为dgExternalValuesWindow。我使用参数SNMPConnection在MainWindow中初始化它的新实例。两个网格只有两列,少于10行。当我试图展示ConnectionManageForm时 我收到了ArgumentOutOfRangeException。我在那里找到了类似的问题讨论:

One

Two

这是一个堆栈跟踪:

System.ArgumentOutOfRangeException was caught
  HResult=-2146233086
  Message=Specified argument was out of the range of valid values.
Parameter name: index
  Source=PresentationCore
  ParamName=index
  StackTrace:
       at System.Windows.Media.VisualCollection.get_Item(Int32 index)
       at System.Windows.Controls.UIElementCollection.get_Item(Int32 index)
       at System.Windows.Controls.UIElementCollection.System.Collections.IList.get_Item(Int32 index)
       at System.Windows.Controls.DataGridCellsPanel.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)
       at System.Windows.Controls.ItemsPresenter.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds)
       at System.Windows.Controls.Primitives.DataGridCellsPresenter.ArrangeOverride(Size finalSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Border.ArrangeOverride(Size finalSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds)
       at System.Windows.Controls.DataGridRow.ArrangeOverride(Size arrangeBounds)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.VirtualizingStackPanel.ArrangeFirstItemInExtendedViewport(Boolean isHorizontal, UIElement child, Size childDesiredSize, Double arrangeLength, Rect& rcChild, Size& previousChildSize, Point& previousChildOffset, Int32& previousChildItemIndex)
       at System.Windows.Controls.VirtualizingStackPanel.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)
       at System.Windows.Controls.ItemsPresenter.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.ScrollContentPresenter.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds)
       at System.Windows.Controls.ScrollViewer.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Border.ArrangeOverride(Size finalSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Control.ArrangeOverride(Size arrangeBounds)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.StackPanel.StackArrangeHelper(IStackMeasure arrangeElement, IStackMeasureScrollData scrollData, Size arrangeSize)
       at System.Windows.Controls.StackPanel.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Grid.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at MS.Internal.Helper.ArrangeElementWithSingleChild(UIElement element, Size arrangeSize)
       at System.Windows.Controls.ContentPresenter.ArrangeOverride(Size arrangeSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Documents.AdornerDecorator.ArrangeOverride(Size finalSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Controls.Border.ArrangeOverride(Size finalSize)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Window.ArrangeOverride(Size arrangeBounds)
       at System.Windows.FrameworkElement.ArrangeCore(Rect finalRect)
       at System.Windows.UIElement.Arrange(Rect finalRect)
       at System.Windows.Interop.HwndSource.SetLayoutSize()
       at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
       at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
       at System.Windows.Window.SetRootVisual()
       at System.Windows.Window.SetRootVisualAndUpdateSTC()
       at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
       at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
       at System.Windows.Window.CreateSourceWindowDuringShow()
       at System.Windows.Window.SafeCreateWindowDuringShow()
       at System.Windows.Window.ShowHelper(Object booleanBox)
       at System.Windows.Window.Show()
       at MYProject
  InnerException: 

我的代码。 DataContext Window属性绑定到Connection属性:

<Label Content="Binary Outputs" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                    <DataGrid x:Name="dgBinaryOutputs" HorizontalAlignment="Left" Margin="0,26,0,0" VerticalAlignment="Top" Grid.Column="0"
                                AutoGenerateColumns="False"  
                                ItemsSource="{Binding Path=DataContext.BinaryOutputs, Mode=TwoWay}" ScrollViewer.CanContentScroll="False"
                                CanUserAddRows="False" CanUserDeleteRows="False" >
                        <DataGridCell Selected="DataGrid_GotFocus"></DataGridCell>
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Name" Binding="{Binding Path=Name, Mode=TwoWay}"  Width="90"/>

                            <DataGridCheckBoxColumn Header="Enabled" Binding="{Binding Path=Enabled, Mode=TwoWay}" Width="90"/>


                        </DataGrid.Columns>

                    </DataGrid>




                    <Label Content="External values" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                    <DataGrid x:Name="dgExternalValues" HorizontalAlignment="Left" Margin="0,26,0,0" VerticalAlignment="Top" Grid.Column="1" 
                                AutoGenerateColumns="False" 
                                ItemsSource="{Binding Path=DataContext.ExternalValues, Mode=TwoWay}" ScrollViewer.CanContentScroll="False"
                                CanUserAddRows="False" CanUserDeleteRows="False">
                        <DataGridCell Selected="DataGrid_GotFocus"></DataGridCell>
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Name" Binding="{Binding Path=Name, Mode=TwoWay}"  Width="90"/>

                            <DataGridTextColumn Header="Value" Binding="{Binding Path=Value, Mode=TwoWay}" Width="90"/>


                        </DataGrid.Columns>

                    </DataGrid>

代码隐藏:

 public partial class ConnectionManageForm : Window
    {

        SNMPConnection Connection { get; set; }

        public ConnectionManageForm(SNMPConnection connection)
        {          

            InitializeComponent();

            Connection = connection;
            this.DataContext = Connection;

            Connection.Start();
            this.Loaded += new RoutedEventHandler(Window_Loaded);


        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = new TreeViewItem();
            Connection.MakeTreeView(ref item);
            TreeGet.Items.Add(item);



            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(1000);
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();

            dgExternalValues.UpdateLayout();
            dgBinaryOutputs.UpdateLayout();

        }

       private  void timer_Tick(object sender, EventArgs e)
         {
             Connection.UpdateModel();

         }

}

这里崩溃了:

private void ManageConnectionHandler(object connId)
        {
            var connection = SNMPConnections.Where(x => x.ID == (Guid)connId).FirstOrDefault();
            ConnectionManageForm manageForm = new ConnectionManageForm(connection);
            try
            {

                manageForm.Show();//Exception appears
            }
            catch (InvalidOperationException ex)
            {
            }
            catch (ArgumentOutOfRangeException ex)
            {


            }
        }

0 个答案:

没有答案