Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

c# - BindingExpression path error

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

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.

EDIT:

The 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));
        }
    }
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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

  • This line means it can't find the property CategoryModel on the object String

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

  • This line contains the Path property for the binding expression that is throwing the error

    BindingExpression:Path=CategoryModel.CategoryList;

  • This line tells you the Source object for the binding that is throwing the error (typically the DataContext)

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

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...