BindingExpression path error

13,853

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

Share:
13,853
mechanicum
Author by

mechanicum

Updated on June 04, 2022

Comments

  • mechanicum
    mechanicum almost 2 years

    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));
            }
        }
    }
    }
    
  • mechanicum
    mechanicum over 11 years
    Yup, exactly the problem. Thanks for the detailed explanations.
  • blindmeis
    blindmeis over 11 years
    +1, and btw when i got binding errors and i think the xaml bindings are ok - then the datacontext is most time the wrong one :) thats why i always check the datacontext first
  • dumbledad
    dumbledad about 9 years
    One other gotcha, if the object is a Task there may be a missing await