wpf BindingExpression 路径错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14481538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BindingExpression path error
提问by mechanicum
There are many similar questions and I've tried a number of answers from those questions but so far nothing helps. I do not understand what the error message means actually. The error message is;
有很多类似的问题,我已经尝试了这些问题的许多答案,但到目前为止没有任何帮助。我不明白错误消息的实际含义。错误信息是;
System.Windows.Data Error: 40 : BindingExpression path error: 'CategoryModel'
property not found on 'object' ''String' (HashCode=-57655201)'.
BindingExpression:Path=CategoryModel.CategoryList; DataItem='String'
(HashCode=-57655201); target element is 'TextBlock' (Name=''); target property is
'Text' (type 'String')
CategoryList contains a string list of categories which are full (checked from debug). My xaml is below,
CategoryList 包含一个完整类别的字符串列表(从调试中检查)。我的xaml在下面,
<ListView x:Name="categoryListView" HorizontalAlignment="Left" Width="56" Height="156"
ItemsSource="{Binding Path=CategoryModel.CategoryList}"
DisplayMemberPath="CategoryModel.CategoryList"
SelectedValue="{Binding Path=CategoryModel.SelectedCategory}"
VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5">
</ListView>
The xaml design looks ok, application runs fine but nothing gets filled. The categoryList is supposed to be filled at initialization. It is filled actually but listView doesn't show anything.
xaml 设计看起来不错,应用程序运行良好但没有填充。categoryList 应该在初始化时填充。它实际上已填充,但 listView 不显示任何内容。
EDIT:
编辑:
The CategoryModel;
类别模型;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RecorderApp.Model
{
public class CategoryModel : INotifyPropertyChanged
{
private String _selectedCategory;
private String _recordTitle;
private String _systemInfoLabel;
private ObservableCollection<String> _categoryList;
public ObservableCollection<String> CategoryList
{
get { return _categoryList; }
set
{
if (_categoryList != value)
{
_categoryList = value;
OnPropertyChanged("CategoryList");
}
}
}
public String SystemInfoLabel
{
get { return _systemInfoLabel; }
set
{
if (_systemInfoLabel != value)
{
_systemInfoLabel = value;
OnPropertyChanged("SystemInfoLabel");
}
}
}
public String SelectedCategory
{
get { return _selectedCategory; }
set
{
if (_selectedCategory != value)
{
_selectedCategory = value;
OnPropertyChanged("SelectedCategory");
}
}
}
public string RecordTitle
{
get { return _recordTitle; }
set
{
_recordTitle = value;
OnPropertyChanged("RecordTitle");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
回答by Rachel
Your DisplayMemberPathbinding is causing the error, and in your case should be removed entirely since it is not needed.
您的DisplayMemberPath绑定导致了错误,在您的情况下,应该完全删除它,因为它不需要。
To use DisplayMemberPath, you need to be able to reference the property like ListView.ItemsSource[X].SomeProperty, where SomePropertywould be your DisplayMemberPath
要使用DisplayMemberPath,您需要能够引用属性,例如ListView.ItemsSource[X].SomeProperty,SomeProperty您的DisplayMemberPath
You are getting this error because your ItemsSourceis a List<String>, and Stringdoes not contain a property called CategoryModel.
您收到此错误是因为您ItemsSource是List<String>,并且String不包含名为 的属性CategoryModel。
To explain the exact binding error you have:
要解释您遇到的确切绑定错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'CategoryModel' property not found on 'object' ''String' (HashCode=-57655201)'. BindingExpression:Path=CategoryModel.CategoryList; DataItem='String' (HashCode=-57655201); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data 错误:40:BindingExpression 路径错误:在“object”“String”(HashCode=-57655201)上找不到“CategoryModel”属性。BindingExpression:Path=CategoryModel.CategoryList; DataItem='String' (HashCode=-57655201); 目标元素是 'TextBlock' (Name=''); 目标属性是“文本”(类型“字符串”)
This line means it can't find the property
CategoryModelon the objectStringBindingExpression path error: 'CategoryModel' property not found on 'object' ''String' (HashCode=-57655201)'
This line contains the
Pathproperty for the binding expression that is throwing the errorBindingExpression:Path=CategoryModel.CategoryList;
This line tells you the Source object for the binding that is throwing the error (typically the
DataContext)DataItem='String' (HashCode=-57655201);
And this line means it is failing to bind the property
Texton aTextBox(DisplayMemberPathis a shortcut way of making theItemTemplatea singleTextBlock, with it'sTextbound to theDisplayMemberPathproperty)target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
这一行意味着它找不到
CategoryModel对象上的属性StringBindingExpression 路径错误:在“object”“String”(HashCode=-57655201)上找不到“CategoryModel”属性
此行包含
Path引发错误的绑定表达式的属性BindingExpression:Path=CategoryModel.CategoryList;
此行告诉您引发错误的绑定的 Source 对象(通常是
DataContext)DataItem='String' (HashCode=-57655201);
而此行意味着它是失败的属性绑定
Text上TextBox(DisplayMemberPath是制作的快捷方式ItemTemplate单一TextBlock,与它的Text绑定DisplayMemberPath属性)目标元素是 'TextBlock' (Name=''); 目标属性是“文本”(类型“字符串”)
So to put it all together, it is telling you that it is trying to bind TextBox.Textto {Binding Path=CategoryModel.CategoryList}, however the DataContextbehind the TextBoxis of type String, and Stringdoes not have a property called CategoryModel
所以把它们放在一起,它告诉你它正在尝试绑定TextBox.Text到{Binding Path=CategoryModel.CategoryList},但是DataContext后面的TextBox类型是String,并且String没有名为的属性CategoryModel
回答by Smaug
The below static binding also may help you.
下面的静态绑定也可以帮助你。
<Window.Resources>
<local:CategoryModel x:Key="objCategory" />
</Window.Resources>
<Grid>
<ListView x:Name="categoryListView"
HorizontalAlignment="Left"
Width="56" Height="156"
ItemsSource="{Binding Source={StaticResource objCategory}, Path=CategoryList}"
VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" />
</Grid>

