BindingExpression路径错误

mec*_*cum 2 c# wpf binding

有很多类似的问题,我从这些问题中尝试了很多答案,但到目前为止没有任何帮助.我不明白错误信息实际意味着什么.错误信息是;

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')
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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));
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

Rac*_*hel 11

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

要使用DisplayMemberPath,您需要能够引用属性,例如ListView.ItemsSource[X].SomeProperty,SomeProperty您的位置DisplayMemberPath

您收到此错误,因为您ItemsSource是a List<String>,并且String不包含名为的属性CategoryModel.

要解释您遇到的确切绑定错误:

System.Windows.Data错误:40:BindingExpression路径错误:'object'''String'(HashCode = -57655201)'上找不到'CategoryModel'属性.BindingExpression:路径= CategoryModel.CategoryList; DataItem ='String'(HashCode = -57655201); target元素是'TextBlock'(Name =''); target属性是'Text'(类型'String')

  • 此行表示无法CategoryModel在对象上找到该属性String

    BindingExpression路径错误:'object'''String'上找不到'CategoryModel'属性(HashCode = -57655201)'

  • 该行包含Path抛出错误的绑定表达式的属性

    BindingExpression:路径= CategoryModel.CategoryList;

  • 这一行告诉你抛出错误的绑定的Source对象(通常是DataContext)

    DataItem ='String'(HashCode = -57655201);

  • 而此行意味着它不能在属性绑定TextTextBox(DisplayMemberPath是制作的快捷方式ItemTemplate单一TextBlock,与它的Text绑定DisplayMemberPath属性)

    target元素是'TextBlock'(Name =''); target属性是'Text'(类型'String')

因此,为了把它放在一起,这是告诉你,它试图绑定TextBox.Text{Binding Path=CategoryModel.CategoryList}然而,DataContext后面TextBox是类型的String,并String没有一个所谓的财产CategoryModel