有很多类似的问题,我尝试了许多来自这些问题的答案,但是到目前为止什么都没有帮助。实际上,我不理解错误消息的含义。错误消息是:
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 包含一组字符串列表,这些列表是完整的(从 debug 中检查得出)。我的 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没有显示任何内容。
编辑:
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));
}
}
}
}