Terje Isaksen Developer blog

2010-05-04 (Tuesday)

WPF: Missing tag gives exception “Items collection must be empty before using ItemsSource.”

Filed under: Developing — Terje Isaksen @ 14:14
Tags: , , ,

If you have a datagrid with columns and misses to use the tag <toolkit:DataGrid.Columns> – then you will get the exception “Items collection must be empty before using ItemsSource.”

Wrong:

<toolkit:DataGrid >
     <toolkit:DataGridTemplateColumn >
        <toolkit:DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
               <StackPanel>
                  <toolkit:DatePicker  SelectedDate="{Binding Date, Mode=Twoway, StringFormat=yyyy-MM-dd, UpdateSourceTrigger=PropertyChanged}" />
                </StackPanel>
           </DataTemplate>
        </toolkit:DataGridTemplateColumn.CellTemplate>
     </toolkit:DataGridTemplateColumn>
</toolkit:DataGrid> 

Correct:

<toolkit:DataGrid >
  <toolkit:DataGrid.Columns>
     <toolkit:DataGridTemplateColumn >
        <toolkit:DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
               <StackPanel>
                  <toolkit:DatePicker  SelectedDate="{Binding Date, Mode=Twoway, StringFormat=yyyy-MM-dd, UpdateSourceTrigger=PropertyChanged}" />
                </StackPanel>
           </DataTemplate>
        </toolkit:DataGridTemplateColumn.CellTemplate>
     </toolkit:DataGridTemplateColumn>
  </toolkit:DataGrid.Columns>
</toolkit:DataGrid> 

Advertisements

Blog at WordPress.com.