Wpf 由于缺少信息,BindingExpression无法检索值
我在使用绑定时遇到问题,我知道该属性存在,但由于某些原因WPF找不到它。打开跟踪,我得到以下错误Wpf 由于缺少信息,BindingExpression无法检索值,wpf,mvvm,data-binding,Wpf,Mvvm,Data Binding,我在使用绑定时遇到问题,我知道该属性存在,但由于某些原因WPF找不到它。打开跟踪,我得到以下错误 System.Windows.Data Error: 40 : BindingExpression path error: 'DefaultInBaseObject' property not found on 'object' ''GcBaseBuildingProperties' (HashCode=971734)'. BindingExpression:Path=DefaultInB
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;
DataTemplate dt=新的DataTemplate
{
DataType=data.GetType()
};
FrameworkElementFactory spFactory=新FrameworkElementFactory(类型为(StackPanel));
spFactory.Name=“测试”;
设置值(StackPanel.OrientationProperty,Orientation.Vertical);
SetValue(StackPanel.DataContextProperty,数据);
//它的开始与往常几乎一样,我们得到了NMSTemplate中所有字段的集合。
IOrderedEnumerable fields=data.GetType().GetFields().OrderBy(field=>field.MetadataToken);
如果(字段!=null)
{
//然后我们在所有这些字段上循环。
foreach(字段中的字段信息字段信息)
{
var attributes=(NMSAttribute[])fieldInfo.GetCustomAttributes(typeof(NMSAttribute),false)//
libMBIN.Models.NMSAttribute attrib=null//
如果(attributes.Length>0)attrib=attributes[0]//
bool ignore=false//
如果(attrib!=null)ignore=attrib.ignore//
if(!ignore)//将该字段添加到mbinContents列表中
{
FrameworkElementFactory标签=新FrameworkElementFactory(typeof(TextBlock));
Label.SetValue(TextBlock.TextProperty,fieldInfo.Name);
Label.SetValue(TextBlock.ToolTipProperty,fieldInfo.Name);
spFactory.AppendChild(标签);
FrameworkElementFactory持卡人=新FrameworkElementFactory(类型(文本块));
持卡人设置绑定(TextBlock.TextProperty,新绑定(fieldInfo.Name));
持卡人设置值(TextBlock.ToolTipProperty,fieldInfo.Name);
spFactory.AppendChild(持卡人);
}
}
dt.VisualTree=spFactory;
}
返回dt;
我已经仔细检查并确保上下文是正确的,对象在那里,并且它包含属性
不幸的是,我并不真正理解这些错误/消息。有人能解释一下吗?您的绑定错误可能是因为您想绑定到字段,而您只能绑定到属性 如果您想在保持动态的同时改进代码,您可以:
这样的代码更容易理解、调试和测试。Hi。在WPF中,真正需要在代码中构建数据模板是很少见的。通常是来自其他技术的人尝试这样做,而不是在XAML中创建数据模板。我相信可能是这样,因为你不理解这些错误。另外,想想你的代码维护人员,他们更难修复或扩展它。嗨,谢谢你的评论,我之所以构建这段代码是因为对对象的无知。我正在使用反射动态构建模板,因此无法在xaml中构建模板,因为属性名称/类型在设计时未知。我已经使用WPF很长一段时间了,我只是不能解决这里的问题。嘿,谢谢你的回复。我不敢相信,但是你说的对,当我扫了一眼全班,看到“publicstringname;”时,我犯了一个愚蠢的错误假设这是一处房产。