绑定表达式路径错误

本文关键字:错误 路径 表达式 绑定 | 更新日期: 2023-09-27 17:55:17

有很多

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

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

类别列表包含已满类别的字符串列表(从调试中检查)。我的 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 设计看起来不错,应用程序运行良好,但没有填充任何东西。类别列表应该在初始化时填充。它实际上已填充,但列表视图没有显示任何内容。

编辑:

类别模型;

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绑定导致了错误,在您的情况下,应完全删除,因为它不需要。

要使用DisplayMemberPath,你需要能够引用像ListView.ItemsSource[X].SomeProperty这样的属性,其中SomeProperty将是你的DisplayMemberPath

您收到此错误是因为您的ItemsSourceList<String>,并且String不包含名为 CategoryModel 的属性。

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

System.Windows.Data 错误: 40: 绑定表达式路径错误: "类别模型" 在"对象"字符串"(哈希代码=-57655201)"上找不到属性。 BindingExpression:Path=CategoryModel.CategoryList;数据项='字符串' (哈希代码=-57655201);目标元素是"文本块"(名称=");目标属性为 "文本"(键入"字符串")

  • 此行表示它找不到对象CategoryModel的属性String

    绑定表达式路径错误:"类别模型" 在"对象"字符串"(哈希代码=-57655201)"上找不到属性

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

    BindingExpression:Path=CategoryModel.CategoryList;

  • 此行告诉您引发错误的绑定的 Source 对象(通常是 DataContext

    数据项='字符串' (哈希代码=-57655201);

  • 这条线意味着它无法将属性Text绑定到TextBox上(DisplayMemberPath是使ItemTemplate成为单个TextBlock的快捷方式,它Text绑定到DisplayMemberPath属性)

    目标元素是"TextBlock"(Name='');目标属性是 "文本"(键入"字符串")

所以把它放在一起,它告诉你它试图将TextBox.Text绑定到{Binding Path=CategoryModel.CategoryList},但是TextBox背后的DataContextString类型,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>