c# - BindingExpression路径错误

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

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));
        }
    }
}
}

最佳答案

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

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

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

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

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

  • 这一行意味着它找不到属性 CategoryModel在对象上 String

    BindingExpression path error: 'CategoryModel' property not found on 'object' ''String' (HashCode=-57655201)'

  • 此行包含 Path抛出错误的绑定(bind)表达式的属性

    BindingExpression:Path=CategoryModel.CategoryList;

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

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

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

    target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

所以把它们放在一起,它告诉你它正在尝试绑定(bind) TextBox.Text{Binding Path=CategoryModel.CategoryList} ,但是 DataContextTextBox后面类型为 String , 和 String没有名为 CategoryModel 的属性

关于c# - BindingExpression路径错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14481538/

相关文章:

c# - 语音合成器文本到语音作为输入,即通过麦克风

c# - 使用 WPF/C# 获取有关处理器的信息

wpf - 停止 WPF ScrollViewer 自动滚动到感知的内容

c# - ComboBox header 不遵守 ItemTemplate

Silverlight UserControl 自定义属性绑定(bind)

c++ - Cocos2d-x lua绑定(bind)

c# - 无法在 Chargify REST API 上进行身份验证

c# - 从 C# 执行 SSIS 包

c# - 在 IIS c# 中启动一个新的工作进程

.net - 一个绑定(bind)中有 2 个属性?