简单场景:

  列表绑定后台数据,点击列表项在view的cs中拿点击项的DataContext进一步处理。正常情况下应该是能拿到我绑定上去的数据,但是偶尔会点出来DisconnectedItem,重现几率不小,大概四五次就能点出一次。

<ItemsControl ItemsSource="{Binding Source}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Path}" MouseLeftButtonUp="Image_OnMouseLeftButtonUp"></Image>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

搜索了一把,能找到的帮助很少,DisconnectedItem应当是逻辑树的一部分,在绑定断开的时候的状态,至于为什么会在正常状态下点出来不得而知。

微软表示在4.5中会修复这个bug

由 Microsoft 在 2011/7/7 16:51 发送
In the next release, we will not raise DataContextChanged when changing to {DisconnectedItem}. We are also providing a way to check for {DisconnectedItem}.
 
我是用的3.5,暂时没有升级计划,有人说4.5也有问题:

由 skst 在 2012/12/11 5:34 发送
This is marked as Closed and Fixed. What, exactly, was the fix? Is a DisconnectedItem no longer being passed to the DataContextChanged event? Is there a way to test if an object is a DisconnectedItem? I'm seeing DisconnectedItem passed to the Loaded event in WPF 4.5, so it seems that this object is still being exposed.

办法找不到,但是bug单还是挂着,总不能被bug单憋死吧,只能各种瞎试,特么还真试出个方法。

<ItemsControl ItemsSource="{Binding Source}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image DataContext="{Binding}" Source="{Binding Path}" MouseLeftButtonUp="Image_OnMouseLeftButtonUp"></Image>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>

在DataTemplate上加一个强制绑定,再也没有重现过DisconnectedItem的问题了。

bug单是走了,但是还不明白内部的原理,不求甚解,呵呵。

最新文章

  1. 什么是Ajax?
  2. 无法打开键: UNKNOWN\Components\BE1FB738077DBE490AF18C3B9B1A1EE8\E5F5286B58B542741A00A0A9AA420B27
  3. C1000k 新思路:用户态 TCP/IP 协议栈
  4. datahub
  5. [Liferay6.2]AUI表单验证示例
  6. CRM客户关系管理系统(十三)
  7. PHP解码unicode编码中文字符代码示例
  8. 如何在DJANGO里,向有外键(一对多和多对多)的DB里插入数据?
  9. directx11编程中遇到的错误及解决方法
  10. visibility: hidden和 display: none的区别
  11. 标注的SQL拼接语句
  12. python位运算
  13. 利用history.pushState()实现页面无刷新更新
  14. 在 Win 7或8 下使用 VirtualBOX 虚拟机安装 OS X 10.11 El Capitan 及 Xcode 7.0
  15. Vijos1605 NOIP2008 提高组T4 双栈排序 BFS
  16. CSS3 Flex布局整理(三)-项目属性
  17. [转载]java中io流关闭的顺序
  18. struts2返回json字符串
  19. 微信小程序 --- page.json文件
  20. 实现IT服务弹性伸缩的利器

热门文章

  1. sql根据表名获取字段及对应说明
  2. PHP读书笔记(2)-变量与数据结构
  3. 第二次作业第2题_JH
  4. 谈谈项目中遇到的各种iOS7适配问题
  5. Linux学习之路:shell变量(一)
  6. ssh 安全配置
  7. Win7:“找不到该项目”错误解决大法
  8. js计算2个日期相差的天数,两个日期相差的天数,日期相隔天数
  9. (译文)12个简单(但强大)的JavaScript技巧(一)
  10. MVVM - 基础介绍