有很多类似的问题,我已经尝试了这些问题的一些答案,但到目前为止没有任何帮助。我不明白错误消息的实际含义。错误信息是;
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 包含已满类别的字符串列表(从调试中检查)。我的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>
xaml 设计看起来不错,应用程序运行良好但没有填充任何内容。 categoryList 应该在初始化时被填充。它实际上已填充,但 listView 不显示任何内容。
编辑:
类别模型;
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));
}
}
}
}
最佳答案
你的 DisplayMemberPath
绑定(bind)导致错误,在您的情况下应该完全删除,因为不需要它。
使用DisplayMemberPath
,您需要能够引用 ListView.ItemsSource[X].SomeProperty
之类的属性, 其中SomeProperty
将是你的 DisplayMemberPath
您收到此错误是因为您的 ItemsSource
是 List<String>
, 和 String
不包含名为 CategoryModel
的属性.
要解释您遇到的确切绑定(bind)错误:
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')
这一行意味着它找不到属性
CategoryModel
在对象上String
BindingExpression path error: 'CategoryModel' property not found on 'object' ''String' (HashCode=-57655201)'
此行包含
Path
抛出错误的绑定(bind)表达式的属性BindingExpression:Path=CategoryModel.CategoryList;
此行告诉您绑定(bind)的 Source 对象引发错误(通常是
DataContext
)DataItem='String' (HashCode=-57655201);
这一行意味着它无法绑定(bind)属性
Text
在TextBox
上(DisplayMemberPath
是使ItemTemplate
成为单个TextBlock
的快捷方式,它的Text
绑定(bind)到DisplayMemberPath
属性)target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
所以把它们放在一起,它告诉你它正在尝试绑定(bind) TextBox.Text
至 {Binding Path=CategoryModel.CategoryList}
,但是 DataContext
在TextBox
后面类型为 String
, 和 String
没有名为 CategoryModel
的属性
关于c# - BindingExpression路径错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14481538/