C# BindingExpression路径错误

C# BindingExpression路径错误,c#,wpf,binding,C#,Wpf,Binding,有许多类似的问题,我试过从这些问题中找到一些答案,但到目前为止没有任何帮助。我不明白这个错误消息实际上是什么意思。错误消息为 System.Windows.Data Error: 40 : BindingExpression path error: 'CategoryModel' property not found on 'object' ''String' (HashCode=-57655201)'. BindingExpression:Path=CategoryModel.Categor

有许多类似的问题,我试过从这些问题中找到一些答案,但到目前为止没有任何帮助。我不明白这个错误消息实际上是什么意思。错误消息为

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));
        }
    }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间RecorderApp.Model
{
公共类类别模型:INotifyPropertyChanged
{
私有字符串_selectedCategory;
私有字符串recordTitle;
私有字符串systemInfoLabel;
私有可观测集合分类列表;
公共可观测集合类别列表
{
获取{return\u categoryList;}
设置
{
if(_categoryList!=值)
{
_类别列表=值;
OnPropertyChanged(“类别列表”);
}
}
}
公共字符串SystemInfoLabel
{
获取{return\u systemInfoLabel;}
设置
{
if(_systemInfoLabel!=值)
{
_systemInfoLabel=值;
OnPropertyChanged(“系统信息标签”);
}
}
}
公共字符串SelectedCategory
{
获取{return\u selectedCategory;}
设置
{
如果(_selectedCategory!=值)
{
_selectedCategory=值;
OnPropertyChanged(“SelectedCategory”);
}
}
}
公共字符串记录标题
{
获取{return\u recordTitle;}
设置
{
_记录标题=值;
不动产变更(“记录所有权”);
}
}
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
}

您的
DisplayMemberPath
绑定导致了错误,在您的情况下,应该完全删除,因为不需要它

要使用
DisplayMemberPath
,您需要能够引用类似
ListView.ItemsSource[X].SomeProperty的属性,其中
SomeProperty
将是您的
DisplayMemberPath

出现此错误是因为您的
ItemsSource
列表
,并且
String
不包含名为
CategoryModel
的属性

要解释确切的绑定错误,请执行以下操作:

System.Windows.Data错误:40:BindingExpression路径错误:“CategoryModel” 在“对象”“字符串”(HashCode=-57655201)上找不到属性。 BindingExpression:Path=CategoryModel.CategoryList;DataItem='String' (HashCode=-57655201);目标元素为“TextBlock”(名称=“”);目标属性为 “文本”(键入“字符串”)

  • 此行意味着它在对象
    字符串

    BindingExpression路径错误:“CategoryModel” 在“对象”“字符串”(HashCode=-57655201)上找不到属性

  • 此行包含引发错误的绑定表达式的
    Path
    属性

    BindingExpression:Path=CategoryModel.CategoryList

  • 这一行告诉您引发错误的绑定的源对象(通常是
    DataContext

    DataItem='String' (HashCode=-57655201)

  • 该行表示无法在
    文本框上绑定属性
    Text
    DisplayMemberPath
    是将
    ItemTemplate
    作为单个
    TextBlock
    的快捷方式,将
    Text
    绑定到
    DisplayMemberPath
    属性)

    目标元素为“TextBlock”(名称=“”);目标属性为 “文本”(键入“字符串”)


因此,总而言之,它告诉您它正在尝试将
TextBox.Text
绑定到
{Binding Path=CategoryModel.CategoryList}
,但是
TextBox
后面的
DataContext
类型是
String
,而且
String
没有名为
CategoryModel

的属性,下面的静态绑定也可以帮助您

<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>


我认为您使用SelectedValue是错误的。尝试将其替换为SelectedValuePathchanged,错误相同。你也需要这些模型吗?描述一下你正在尝试做什么,因为它不清楚。您希望项目是什么来源要显示的内容和要选择的内容值。我这样问是因为:CategoryList是一个可观察的字符串集合。由于您将ItemSource设置为此可观察集合DisplayMemberPath和SelectedValue(路径或否)没有任何用处。ItemsSource是我计划在listView上显示的字符串列表。我不知道是否需要DisplayMemberPath来显示这样的列表,但我想为什么不需要呢?SelectedValue绑定ListView的选定元素。仅此而已。检查我的答案我想我已经找到了(我从第一篇帖子开始编辑了它,所以再次检查)是的,正是问题所在。感谢您的详细解释。+1,顺便说一句,当我遇到绑定错误时,我认为xaml绑定还可以-那么datacontext大多数时候是错误的:)这就是为什么我总是先检查datacontext,然后再检查另一个,如果对象是
任务
可能缺少
等待