BindingExpression 路径错误 [英] BindingExpression path error

查看:13
本文介绍了BindingExpression 路径错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多类似的问题,我已经尝试了这些问题的许多答案,但到目前为止没有任何帮助.我不明白错误消息的实际含义.错误信息是;

There are many similar questions and I've tried a number of answers from those questions but so far nothing helps. I do not understand what the error message means actually. The error message is;

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在下面,

CategoryList contains a string list of categories which are full (checked from debug). My xaml is below,

<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 没有显示任何内容.

The xaml design looks ok, application runs fine but nothing gets filled. The categoryList is supposed to be filled at initialization. It is filled actually but listView doesn't show anything.

类别模型;

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

Your DisplayMemberPath binding is causing the error, and in your case should be removed entirely since it is not needed.

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

To use DisplayMemberPath, you need to be able to reference the property like ListView.ItemsSource[X].SomeProperty, where SomeProperty would be your DisplayMemberPath

您收到此错误是因为您的 ItemsSourceList,并且 String 不包含名为 的属性类别模型.

You are getting this error because your ItemsSource is a List<String>, and String does not contain a property called CategoryModel.

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

To explain the exact binding error you have:

System.Windows.Data 错误:40:BindingExpression 路径错误:'CategoryModel'在对象"字符串"(HashCode=-57655201)上找不到属性.BindingExpression:Path=CategoryModel.CategoryList;数据项='字符串'(哈希码=-57655201);目标元素是 'TextBlock' (Name='');目标属性是'文本'(类型'字符串')

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

  • 这一行表示在对象String

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

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

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

    BindingExpression:Path=CategoryModel.CategoryList;

    BindingExpression:Path=CategoryModel.CategoryList;

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

    数据项='字符串'(HashCode=-57655201);

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

  • 这行意味着它无法在 TextBox 上绑定属性 Text(DisplayMemberPath 是一种快捷方式ItemTemplate 单个 TextBlock,它的 Text 绑定到 DisplayMemberPath 属性)

  • And this line means it is failing to bind the property Text on a TextBox (DisplayMemberPath is a shortcut way of making the ItemTemplate a single TextBlock, with it's Text bound to the DisplayMemberPath property)

    目标元素是'TextBlock' (Name='');目标属性是'文本'(类型'字符串')

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

  • 所以把它们放在一起,它告诉你它正在尝试将 TextBox.Text 绑定到 {Binding Path=CategoryModel.CategoryList},但是 TextBox.TextTextBox 后面的code>DataContext 是String 类型,并且String 没有名为CategoryModel

    So to put it all together, it is telling you that it is trying to bind TextBox.Text to {Binding Path=CategoryModel.CategoryList}, however the DataContext behind the TextBox is of type String, and String does not have a property called CategoryModel

    这篇关于BindingExpression 路径错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆