windows 8 MSDN Examples 中一个奇怪的问题
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 items 19 in the grid re-flow into position when one of the child items 20 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>中的方块直接都删除,任然可以运行。
不知有没有人也遇到了类似的问题,这到底是什么原因呢?
作者:成宇佳
博客主页:http://www.cnblogs.com/chengyujia/
欢迎转载,但请保留作者和本文链接,谢谢!
欢迎在下面的评论区与我交流。
博客主页:http://www.cnblogs.com/chengyujia/
欢迎转载,但请保留作者和本文链接,谢谢!
欢迎在下面的评论区与我交流。
如果觉得写的不错,请点击下面的“推荐”按钮,让我更有动力写出更好的文章。
分类:
Windows 8
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?