绑定表达式路径错误

3

有很多类似的问题,我尝试了许多来自这些问题的答案,但是到目前为止什么都没有帮助。实际上,我不理解错误消息的含义。错误消息是:

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 包含一组字符串列表,这些列表是完整的(从 debug 中检查得出)。我的 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));
        }
    }
}
}

我认为你使用 SelectedValue 的方式是错误的。尝试将其替换为 SelectedValuePath。 - iltzortz
更改了它,但错误仍然存在。您需要模型吗? - mechanicum
请进一步描述您正在尝试做什么,因为目前不太清楚。您想要什么作为Itemssource展示,以及选择的值是什么。我之所以问这些问题,是因为CategoryList是一个字符串的可观察集合。由于您将ItemSource设置为此可观察集合,DisplayMemberPath和SelectedValue(路径或非路径)都没有用处。 - iltzortz
ItemsSource是一个字符串列表,我计划在listView上显示它。我不知道是否需要DisplayMemberPath来显示这样的列表,但我觉得为什么不用呢?SelectedValue绑定了ListView中选定的元素。就这些。 - mechanicum
检查我的答案,我认为我明白了(我编辑过了,所以再次检查一下)。 - iltzortz
2个回答

11

DisplayMemberPath绑定引起了错误,在你的情况下应该被完全删除,因为它不需要。

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

您之所以会得到此错误,是因为您的ItemsSource是一个List<String>,而String不包含名为CategoryModel的属性。

要解释您具体的绑定错误:

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上找到属性CategoryModel

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

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

    BindingExpression:Path=CategoryModel.CategoryList;

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

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

  • 这一行的意思是它无法绑定TextBox上的Text属性(DisplayMemberPath是将ItemTemplate设置为单个TextBlock的快捷方式,并将其Text绑定到DisplayMemberPath属性)。

    目标元素是'TextBlock'(名称=''); 目标属性是'Text'(类型'String')

  • 因此,总结起来,它告诉你它正在尝试将TextBox.Text绑定到{Binding Path=CategoryModel.CategoryList},但是TextBox背后的DataContext类型是String,而String没有名为CategoryModel的属性。


    是的,正是这个问题。感谢您提供详细的解释。 - mechanicum
    +1,顺便说一下,当我遇到绑定错误并且我认为XAML绑定是正确的 - 那么数据上下文大多数情况下是错误的 :) 这就是为什么我总是先检查数据上下文的原因。 - blindmeis
    另一个需要注意的是,如果对象是“Task”,可能会缺少“await”。 - dumbledad

    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>
    

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接