WPF 数据绑定和类类型生成 System.Windows.Data 警告:17 个广告错误

问题描述

情况如下:

  1. 我有一个包含一些数据和虚拟字段的类 TItem
  2. 然后我有 2 个类是 TItem 的“子类”但不同:TPtient、TVisit。 (TPatient:TItem 和 TVisit:TItem)第一个是 mPatient Paz,第二个是 mVisit Visit,其中 mPatient 和 mVisit 是具有详细数据的模型。
  3. 在树视图详细信息中,我添加一个模板选择器,一个用于患者,另一个用于访问

当我创建树时,有些项是 TPatient,有些项是 TVisit,但它们都在 TreeView.Items.Add(TItem) 树中声明为 TItem 来处理它们。因此,绑定是针对 TPatient.date 和 TVisit.date 之类的字段,其中日期是 TItem 中的公共虚拟,并被 TPatient.Paz.Name 或 TVisit.Visit.Location 覆盖,这些字段是记录本身的特定字段并且不存在在 TItem 中作为虚拟。

当我在一个和另一个之间单击时,数据显示正确。

问题在于:

   private void select_selecteditemchanged(object sender,System.Windows.RoutedPropertyChangedEventArgs<object> e)
    {
        var tvi = GetContainerFromItem(treeView,e.NewValue);

        _selected = tvi;
        if (_selected != null)
        {
            try
            {
                _selected_parent = (TreeViewItem)ItemsControl.ItemsControlFromItemContainer(tvi);
            }
            catch
            {
                _selected_parent = null;
            }
        }
        SimpleIoc.Default.GetInstance<MasterDetailviewmodel>().Selected = (MTreeViewBase)e.NewValue;
    }

执行时:

SimpleIoc.Default.GetInstance<MasterDetailviewmodel>().Selected = (MTreeViewBase)e.NewValue;

我收到类似的错误

    System.Windows.Data information: 21 : BindingExpression cannot retrieve value from null data item. This Could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=Selected.(0).DataS; DataItem='MasterDetailviewmodel' (HashCode=32218335); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Warning: 17 : Cannot get 'Visit' value (type 'TVisit') from 'Selected' (type 'MTreeViewPaz'). BindingExpression:Path=Selected.(0).(1); DataItem='MasterDetailviewmodel' (HashCode=32218335); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') TargetException:'System.Reflection.TargetException: Object does not match target type.
   at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj,BindingFlags invokeAttr,Binder binder,Object[] parameters,CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj,Object[] index,Object[] index)
   at MS.Internal.Data.PropertyPathWorker.GetValue(Object item,Int32 level)
   at MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)'

对于我绑定的每个字段......在某些情况下是 30 或更多 :( 详细阅读它,错误不是出现在公共字段上,而是出现在与特定 .Paz 或 .Visit 绑定的字段上,因为 .Visit 不存在,以防我们在患者中,反之亦然。

这不是阻塞,但我很想避免它发生。

在我看来,顺序是:

  1. 选定的已更新
  2. 更新和评估现有的内容控件/数据模板绑定
  3. 产生的所有错误
  4. contentcontrol/datatemplate 绑定更新为正确的类型
  5. 重新评估绑定 - 未显示错误 - 数据正确显示

有没有办法避免 2?

我尝试在 TItem 中的任何情况下将它们添加为 Virtual 并保留为 null 但错误在这种情况下它确实存在 TVisit.Paz.Name 但它不起作用因为 Paz 为 null因为我们正在访问。

感谢您的帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)