BindingExpression由于缺少信息而无法检索值

时间:2018-08-25 11:11:01

标签: wpf mvvm data-binding

我遇到绑定问题,我知道该属性存在,但是由于某些原因WPF无法找到它。打开跟踪,出现以下错误

    System.Windows.Data Error: 40 : BindingExpression path error: 'DefaultInBaseObject' property not found on 'object' ''GcBaseBuildingProperties' (HashCode=971734)'. 
BindingExpression:Path=DefaultInBaseObject; DataItem='GcBaseBuildingProperties' (HashCode=971734); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=DefaultInBaseObject; DataItem='GcBaseBuildingProperties' (HashCode=971734);
 target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')


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=DefaultInBaseObject; DataItem='GcBaseBuildingProperties' (HashCode=971734); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') 

我正在使用以下代码构建绑定:

  DataTemplate dt = new DataTemplate
        {
            DataType = data.GetType()
        };

        FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(StackPanel));
        spFactory.Name = "Test";
        spFactory.SetValue(StackPanel.OrientationProperty, Orientation.Vertical);
        spFactory.SetValue(StackPanel.DataContextProperty, data);


        //It starts pretty much the same as usual, we get a collection of all the fields in the NMSTemplate.
        IOrderedEnumerable<FieldInfo> fields = data.GetType().GetFields().OrderBy(field => field.MetadataToken);
        if (fields != null)
        {
            //We then loop over all those fields.
            foreach (FieldInfo fieldInfo in fields)
            {
                var attributes = (NMSAttribute[])fieldInfo.GetCustomAttributes(typeof(NMSAttribute), false);                        //
                libMBIN.Models.NMSAttribute attrib = null;                                                                          //
                if (attributes.Length > 0) attrib = attributes[0];                                                                  //
                bool ignore = false;                                                                                                //
                if (attrib != null) ignore = attrib.Ignore;                                                                         //

                if (!ignore)                                                                                                        // Add the field to the mbinContents list
                {
                    FrameworkElementFactory Label = new FrameworkElementFactory(typeof(TextBlock));
                    Label.SetValue(TextBlock.TextProperty, fieldInfo.Name);
                    Label.SetValue(TextBlock.ToolTipProperty, fieldInfo.Name);
                    spFactory.AppendChild(Label);

                    FrameworkElementFactory cardHolder = new FrameworkElementFactory(typeof(TextBlock));
                    cardHolder.SetBinding(TextBlock.TextProperty, new Binding(fieldInfo.Name));
                    cardHolder.SetValue(TextBlock.ToolTipProperty, fieldInfo.Name);
                    spFactory.AppendChild(cardHolder);
                }
            }
            dt.VisualTree = spFactory;
        }
        return dt;

我已逐步完成并确保上下文正确,对象在其中并且包含属性。

不幸的是,我不太了解这些错误/消息。有人可以解释吗?

1 个答案:

答案 0 :(得分:0)

您遇到的绑定错误可能是因为您想绑定到字段,而只能绑定到属性。

如果您想使代码更好,同时仍保持动态,可以例如:

  1. 读取视图模型中的数据,然后将ExpandoObject用作 DataContext,
  2. 在XAML中,使用ItemsContainer而不是StackPanel(这是WPF代码的一种味道),
  3. 要有一个动态的TextBox,它有可能 按属性名称绑定,创建一个UserControl,该控件公开字符串 名称,并在设置此属性时进行动态绑定。然后 在XAML中使用此UserControl。 (您也可以扩展TextBlock而不是创建UserControl)。

理解,调试和测试这样的代码会容易得多。