【问题标题】:BindingExpression path errorBindingExpression 路径错误
【发布时间】:2013-01-23 13:59:59
【问题描述】:

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

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 包含一个完整类别的字符串列表(从调试中检查)。我的 xml 在下面,

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

编辑:

类别模型;

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 替换它
  • 改了,还是一样。你也需要模型吗?
  • 进一步描述您正在尝试做的事情,因为不清楚。你想成为什么Itemssource 要显示什么以及要选择什么值。我问这些是因为: CategoryList 是一个 Observable 字符串集合。由于您将 ItemSource 设置为此 Observable 集合 DisplayMemberPath 和 SelectedValue(路径与否)没有用。
  • ItemsSource 是我计划在 listView 上显示的字符串列表。我不知道是否需要 DisplayMemberPath 来显示这样的列表,但我想为什么不呢? SelectedValue 绑定 ListView 的选定元素。就是这样。
  • 检查我的答案,我想我已经知道了(我在第一次发帖后编辑了它,所以再检查一次)

标签: c# wpf binding


【解决方案1】:

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

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

您收到此错误是因为您的ItemsSource 是List&lt;String&gt;,而String 不包含名为CategoryModel 的属性。

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

System.Windows.Data 错误:40:BindingExpression 路径错误:'CategoryModel' 在 'object' ''String' (HashCode=-57655201)' 上找不到属性。 BindingExpression:Path=CategoryModel.CategoryList;数据项='字符串' (哈希码=-57655201);目标元素是'TextBlock'(名称='');目标属性是 “文本”(输入“字符串”)

  • 这行表示在对象String上找不到属性CategoryModel

    BindingExpression 路径错误:“CategoryModel” 在 'object' ''String' (HashCode=-57655201)' 上找不到属性

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

    BindingExpression:Path=CategoryModel.CategoryList;

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

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

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

    目标元素是'TextBlock' (Name='');目标属性是 “文本”(输入“字符串”)

总而言之,它告诉您它正在尝试将TextBox.Text 绑定到{Binding Path=CategoryModel.CategoryList},但是TextBox 后面的DataContext 是String 类型,而String 是没有名为CategoryModel的属性

【讨论】:

  • 是的,正是问题所在。感谢您的详细解释。
  • +1,顺便说一句,当我遇到绑定错误并且我认为 xaml 绑定正常时 - 然后数据上下文大部分时间是错误的 :) 这就是为什么我总是先检查数据上下文
  • 另一个问题,如果对象是Task,则可能缺少await
【解决方案2】:

下面的静态绑定也可以帮到你。

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

【讨论】:

    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多