Accordion setting DataContext gives exception

Jun 9, 2010 at 1:15 PM
Edited Jun 30, 2010 at 12:27 PM

Hi all,

 

I hope somebody can explain why the following code gives me an InvalidOperationException (Operation is not valid while ItemCollection has no inner collection. ItemCollection is uninitialized or binding on ItemsControl.ItemSource supplied null for collection.), when clicking the button?

 

     public partial class Window2
    {
        public Window2()
        {
            InitializeComponent();

            accordion1.DataContext = new MyItems();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            accordion1.DataContext = null;
        }
    }

    public class MyItems
    {
        public List<string> Items
        {
            get
            {
                return new List<string> { "Test1", "Test2", "Test3" };
            }
        }
    }

XAML:

<Window x:Class="WpfApplication.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300" xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit">
    <Grid>
        <my:Accordion Name="accordion1" DataContext="{Binding}" ItemsSource="{Binding Path=Items}" HorizontalAlignment="Stretch" Height="230" VerticalAlignment="Top" />
        <Button Height="28" HorizontalAlignment="Right" Name="button1" VerticalAlignment="Bottom" Width="56" Click="button1_Click">Button</Button>
    </Grid>
</Window>

 


 

Kind regards,

Berthold

Jun 30, 2010 at 12:25 PM

Ok, I found the bug in the Accordion class. In the file Accordion.cs in line number 1158 in method "UnselectItem(int index, object item)" the following code can be found:

            if (index < 0 || index > Items.Count)
            {
                // invalid
                return;
            }

This must be changed in:

            if (index < 0 || index > Items.Count || Items.Count == 0)
            {
                // invalid index or no items to unselect
                return;
            }

Because when switching databinding source (or setting it to null) while an item was selected the library crashes because the Items collection has no items while the first item is tried to received in the same
above metioned method in line 1173.

Please fix this in one of the future releases!

Kind regards,
Berthold Hutten