【发布时间】:2013-01-23 13:59:59
【问题描述】:
有很多类似的问题,我已经尝试了这些问题的许多答案,但到目前为止没有任何帮助。我不明白错误消息的实际含义。错误信息是;
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 包含一个完整类别的字符串列表(从调试中检查)。我的 xml 在下面,
<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));
}
}
}
}
【问题讨论】:
-
我认为您对 SelectedValue 的使用是错误的。尝试用 SelectedValuePath 替换它
-
改了,还是一样。你也需要模型吗?
-
进一步描述您正在尝试做的事情,因为不清楚。你想成为什么Itemssource 要显示什么以及要选择什么值。我问这些是因为: CategoryList 是一个 Observable 字符串集合。由于您将 ItemSource 设置为此 Observable 集合 DisplayMemberPath 和 SelectedValue(路径与否)没有用。
-
ItemsSource 是我计划在 listView 上显示的字符串列表。我不知道是否需要 DisplayMemberPath 来显示这样的列表,但我想为什么不呢? SelectedValue 绑定 ListView 的选定元素。就是这样。
-
检查我的答案,我想我已经知道了(我在第一次发帖后编辑了它,所以再检查一次)