Re: IndexOutOfRangeException when clearing C1TreeView

Silverlight

Collaborate with ComponentOne technical experts and fellow developers.

IndexOutOfRangeException when clearing C1TreeView

  • rated by 0 users
  • This post has 1 Reply |
  • 1 Follower
  •  Hi

    I have build a treeview using databinding and a HierachialDatatemplate. Furthermore i have made a controltemplate for the C1TreeViewItem whos IsSelected property is bound to a Selected property on the objects to from which the nodes are created.

    The problem occurs when i try to update the tree. I need to clear it to fill it again with new nodes. When i clear the collection behind the tree, the treeview component throws an IndexOutOfRangeException. I can tell from the stacktrace that it is caused be the IsSelected binding i have created. If i catch the exception the tree is created with the new nodes, but the selected binding no longer work and SelectionChanged event is no longer fired from the tree

    Any suggestions as to how to solve this problem? I thought of traversing the tree and clearing all the bindings, but it seems cumbersome...

    Thanks, best regards

    Thomas

    Stacktrace

         mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument argument = index, System.ExceptionResource resource = ArgumentOutOfRange_Index) + 0x70 bytes   
         mscorlib.dll!System.ThrowHelper.ThrowArgumentOutOfRangeException() + 0x21 bytes   
         mscorlib.dll!System.Collections.Generic.List<C1.Silverlight.C1TreeViewItem>.this[int].get(int index = -1) + 0x1d bytes   
         C1.Silverlight!C1.Silverlight.C1TreeView._selectionManager_SelectionChanged(object sender = {C1.Silverlight.C1RangeSelectionManager}, C1.Silverlight.SelectionChangedEventArgs<int> e = {C1.Silverlight.SelectionChangedEventArgs<int>}) + 0x158 bytes   
         C1.Silverlight!C1.Silverlight.C1RangeSelectionManager.OnSelectionChanged(C1.Silverlight.C1OrderedSet addedItems = {[-1]}, C1.Silverlight.C1OrderedSet removedItems = {[]}) + 0x98 bytes   
         C1.Silverlight!C1.Silverlight.C1RangeSelectionManager.PerformSelection(C1.Silverlight.C1OrderedSet addedItems = {[-1]}, C1.Silverlight.C1OrderedSet removedItems = {[]}) + 0x8d bytes   
         C1.Silverlight!C1.Silverlight.C1RangeSelectionManager.HandleSingleSelection(int index = -1) + 0x18e bytes   
         C1.Silverlight!C1.Silverlight.C1RangeSelectionManager.HandleSelectionChange(int index = -1, bool ignoreKeyboard = true) + 0x65 bytes   
         C1.Silverlight!C1.Silverlight.C1TreeView.HandleSelectionChange(C1.Silverlight.C1TreeViewItem item = {Analytics.Silverlight.HostPages.CustomC1TreeViewItem}, bool statusChanged = true) + 0x8e bytes   
         C1.Silverlight!C1.Silverlight.C1TreeViewItem.HandleIsSelectedPropertyChanged() + 0x43 bytes   
         C1.Silverlight!C1.Silverlight.C1TreeViewItem.OnIsSelectedChanged(bool oldValue = true) + 0x23 bytes   
         C1.Silverlight!C1.Silverlight.C1TreeViewItem.OnIsSelectedPropertyChanged(System.Windows.DependencyObject d = {Analytics.Silverlight.HostPages.CustomC1TreeViewItem}, System.Windows.DependencyPropertyChangedEventArgs e = {System.Windows.DependencyPropertyChangedEventArgs}) + 0x9f bytes   
         System.Windows.dll!System.Windows.DependencyObject.SetValueInternal(System.Windows.DependencyProperty dp = {System.Windows.CustomDependencyProperty}, object value = false, bool allowReadOnlySet = false, bool isSetByStyle = false, bool isSetByBuiltInStyle = false) + 0x4af bytes   
         System.Windows.dll!System.Windows.DependencyObject.SetValueInternal(System.Windows.DependencyProperty dp, object value) + 0x21 bytes   
         System.Windows.dll!System.Windows.DependencyObject.SetValue(System.Windows.DependencyProperty dp, object value) + 0x1b bytes   
         C1.Silverlight!C1.Silverlight.C1TreeViewItem.IsSelected.set(bool value = false) + 0x58 bytes   
         C1.Silverlight!C1.Silverlight.C1TreeView._selectionManager_SelectionChanged(object sender = {C1.Silverlight.C1RangeSelectionManager}, C1.Silverlight.SelectionChangedEventArgs<int> e = {C1.Silverlight.SelectionChangedEventArgs<int>}) + 0x2bb bytes   
         C1.Silverlight!C1.Silverlight.C1RangeSelectionManager.OnSelectionChanged(C1.Silverlight.C1OrderedSet addedItems = {[]}, C1.Silverlight.C1OrderedSet removedItems = {[2]}) + 0x98 bytes   
         C1.Silverlight!C1.Silverlight.C1RangeSelectionManager.PerformSelection(C1.Silverlight.C1OrderedSet addedItems = {[]}, C1.Silverlight.C1OrderedSet removedItems = {[2]}) + 0x8d bytes   
         C1.Silverlight!C1.Silverlight.C1RangeSelectionManager.ClearSelection() + 0xba bytes   
         C1.Silverlight!C1.Silverlight.C1TreeView.OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e = {System.Collections.Specialized.NotifyCollectionChangedEventArgs}) + 0x12c bytes   
         System.Windows.dll!System.Windows.Controls.ItemsControl.RaiseOnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + 0x1a bytes   
         System.Windows.dll!System.Windows.Controls.ItemsControl.ItemsSourceChanged(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e) + 0x14c bytes   
         System.Windows.dll!System.Windows.DependencyObject.RaisePropertyChangeNotifications(System.Windows.DependencyProperty dp = {System.Windows.CustomDependencyProperty}, object newValue = null, object oldValue = Count = 3) + 0x98 bytes   
         System.Windows.dll!System.Windows.DependencyObject.SetValueInternal(System.Windows.DependencyProperty dp = {System.Windows.CustomDependencyProperty}, object value = null, bool allowReadOnlySet = false, bool isSetByStyle = false, bool isSetByBuiltInStyle = false) + 0x714 bytes   
         System.Windows.dll!System.Windows.DependencyObject.SetValueInternal(System.Windows.DependencyProperty dp, object value) + 0x21 bytes   
         System.Windows.dll!System.Windows.DependencyObject.SetValue(System.Windows.DependencyProperty dp, object value) + 0x1b bytes   
         System.Windows.dll!System.Windows.Controls.ItemsControl.ItemsSource.set(System.Collections.IEnumerable value) + 0x7e bytes   
    >    Analytics.Silverlight!Analytics.Silverlight.HostPages.ApplicationMenu.HandleChangedUserContext(Analytics.Silverlight.IUserContext userContext = {Analytics.Silverlight.UserContext}) Line 106 + 0xf bytes    C#
         Analytics.Silverlight!Analytics.Silverlight.HostPages.ApplicationMenu.Setup.AnonymousMethod(object sender = {Analytics.Silverlight.UserContext}, System.EventArgs args = {System.EventArgs}) Line 76 + 0x11 bytes    C#
         Analytics.Silverlight!Analytics.Silverlight.UserContext.OnUserContextChanged() Line 157 + 0x14 bytes    C#
         Analytics.Silverlight!Analytics.Silverlight.UserContext.UserLoggedOut() Line 201 + 0x8 bytes    C#
     

     Selected bindings made from derived classes

    public class CustomC1TreeView : C1TreeView
      {
        protected override DependencyObject GetContainerForItemOverride()
        {
          CustomC1TreeViewItem tvi = new CustomC1TreeViewItem();
          object style = ResourceFinder.FindResource(this, "LeafTreeViewItemStyle");
          tvi.OverrideStyle = style as Style;

          Binding expandedBinding = new Binding("Expanded");
          expandedBinding.Mode = BindingMode.TwoWay;
          tvi.SetBinding(CustomC1TreeViewItem.IsExpandedProperty, expandedBinding);

          //Binding selectedBinding = new Binding("Selected");
          //selectedBinding.Mode = BindingMode.TwoWay;
          //tvi.SetBinding(CustomC1TreeViewItem.IsSelectedProperty, selectedBinding);

          Binding levelBackgroundBinding = new Binding("Level");
          levelBackgroundBinding.Mode = BindingMode.OneWay;
          levelBackgroundBinding.Converter = new LevelToColorConverter();
          tvi.SetBinding(CustomC1TreeViewItem.BackgroundProperty, levelBackgroundBinding);

          Binding levelFontWeightBinding = new Binding("Level");
          levelFontWeightBinding.Mode = BindingMode.OneWay;
          levelFontWeightBinding.Converter = new LevelToFontWeightConverter();
          tvi.SetBinding(CustomC1TreeViewItem.FontWeightProperty, levelFontWeightBinding);

          return tvi;
        }

        //public override void OnApplyTemplate()
        //{
        //  object style = Extensions.FindResource(this, "RootTreeViewItemStyle");
        //  ItemContainerStyle = style as Style;
        //  base.OnApplyTemplate();
        //}

      }

      public class CustomC1TreeViewItem : C1TreeViewItem
      {
        public Style OverrideStyle { get; set; }

        protected override DependencyObject GetContainerForItemOverride()
        {
          CustomC1TreeViewItem tvi = new CustomC1TreeViewItem();

          Binding expandedBinding = new Binding("Expanded");
          expandedBinding.Mode = BindingMode.TwoWay;
          tvi.SetBinding(IsExpandedProperty, expandedBinding);

          Binding selectedBinding = new Binding("Selected");
          selectedBinding.Mode = BindingMode.TwoWay;
          tvi.SetBinding(IsSelectedProperty, selectedBinding);

          Binding levelBinding = new Binding("Level");
          levelBinding.Mode = BindingMode.OneWay;
          levelBinding.Converter = new LevelToColorConverter();
          tvi.SetBinding(BackgroundProperty, levelBinding);

          Binding levelFontWeightBinding = new Binding("Level");
          levelFontWeightBinding.Mode = BindingMode.OneWay;
          levelFontWeightBinding.Converter = new LevelToFontWeightConverter();
          tvi.SetBinding(FontWeightProperty, levelFontWeightBinding);

          return tvi;
        }

        public override void OnApplyTemplate()
        {
          if (OverrideStyle != null)
            ItemContainerStyle = OverrideStyle;
          base.OnApplyTemplate();
        }
      }

     

  • Seems to be issues reported here (see the suggested workaround).

    Regards

    Follow me: Twitter.com/leovernazza/

Page 1 of 1 (2 items)