BindingExpression路径错误

时间:2013-01-23 14:00:00

标签: 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没有显示任何内容。

编辑:

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

2 个答案:

答案 0 :(得分:11)

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

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

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

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

  

System.Windows.Data错误:40:BindingExpression路径错误:'CategoryModel'   在'object'''String'(HashCode = -57655201)'上找不到属性。   BindingExpression:路径= CategoryModel.CategoryList;的DataItem = '字符串'   (的HashCode = -57655201); target元素是'TextBlock'(Name ='');目标属性是   'Text'(输入'String')

  • 此行表示无法在对象CategoryModel上找到属性String

      

    BindingExpression路径错误:'CategoryModel'   在'object'''String'(HashCode = -57655201)'

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

      

    BindingExpression:路径= CategoryModel.CategoryList;

  • 此行告诉您抛出错误的绑定的Source对象(通常为DataContext

      

    的DataItem = '字符串'   (的HashCode = -57655201);

  • 这一行意味着它无法绑定Text上的属性TextBoxDisplayMemberPath是一种使ItemTemplate成为TextBlock的快捷方式{1}},其中Text绑定到DisplayMemberPath属性

      

    目标元素是'TextBlock'(Name ='');目标属性是   'Text'(输入'String')

所以要把它们放在一起,它告诉你它正试图将TextBox.Text绑定到{Binding Path=CategoryModel.CategoryList},但DataContext后面的TextBox属于String {1}},String没有名为CategoryModel

的属性

答案 1 :(得分:0)

以下静态绑定也可以帮助您。

<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>
相关问题