http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.animation.repositionthemetransition.aspx
1 <Button Content="Remove Rectangle" Click="RemoveButton_Click"/> 2 3 <ItemsControl Grid.Row="1" x:Name="rectangleItems"> 4 <ItemsControl.ItemContainerTransitions> 5 <TransitionCollection> 6 7 <!-- Without this, there would be no animation when items 8 are removed. --> 9 <RepositionThemeTransition/>10 </TransitionCollection>11 </ItemsControl.ItemContainerTransitions>12 <ItemsControl.ItemsPanel>13 <ItemsPanelTemplate>14 <WrapGrid Height="400"/>15 </ItemsPanelTemplate>16 </ItemsControl.ItemsPanel>17 18 <!-- All these rectangles are just to demonstrate how the items19 in the grid re-flow into position when one of the child items20 are removed. -->21 <ItemsControl.Items>22 <Rectangle Fill="Red" Width="100" Height="100" Margin="10"/>23 <Rectangle Fill="Red" Width="100" Height="100" Margin="10"/>24 <Rectangle Fill="Red" Width="100" Height="100" Margin="10"/>25 <Rectangle Fill="Red" Width="100" Height="100" Margin="10"/>26 <Rectangle Fill="Red" Width="100" Height="100" Margin="10"/>27 <Rectangle Fill="Red" Width="100" Height="100" Margin="10"/>28 <Rectangle Fill="Red" Width="100" Height="100" Margin="10"/>29 <Rectangle Fill="Red" Width="100" Height="100" Margin="10"/>30 <Rectangle Fill="Red" Width="100" Height="100" Margin="10"/>31 </ItemsControl.Items>32 </ItemsControl>
1 private void RemoveButton_Click(object sender, RoutedEventArgs e)2 {3 if (rectangleItems.Items.Count > 0)4 rectangleItems.Items.RemoveAt(0);5 }
每點擊一次按鈕,將刪除一個紅色的方塊。但當方塊刪除的只剩一個時,再點擊按鈕刪除時會報錯“Value does not fall within the expected range.”
很奇怪的提示,明明還有一個,怎麼就刪不掉呢?難道rectangleItems.Items不可為空嗎?實驗一下,調用rectangleItems.Items.Clear()方法刪除rectangleItems.Items中的所有項,反應正常;又試了下,在XAML中<ItemsControl.Items>中的方塊直接都刪除,任然可以運行。
不知有沒有人也遇到了類似的問題,這到底是什麼原因呢?