2013-01-23 212 views
2

有很多类似的问题,我已经尝试了一些这些问题的答案,但迄今没有任何帮助。我不明白这个错误信息实际上是什么意思。错误信息是;BindingExpression路径错误

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

我认为您对SelectedValue的使用是错误的。尝试用SelectedValuePath替换它 – iltzortz

+0

将其更改并且错误是相同的。你是否也需要这些模型? – mechanicum

+0

描述你想做什么,因为它不清楚。你想成为Itemssource显示什么和选择什么值。我问这些是因为:CategoryList是一个可观察的字符串集合。由于您将ItemSource设置为此Observable集合DisplayMemberPath和SelectedValue(Path或Not)是没有用的。 – iltzortz

回答

11

DisplayMemberPath结合导致错误,并在你的情况应该被完全删除,因为它不需要。

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

您收到此错误,因为你的ItemsSourceList<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')

  • 这条线意味着它不能找物业CategoryModel对象上String

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

  • 此行包含Path属性为抛出错误的绑定表达式

    BindingExpression:Path=CategoryModel.CategoryList;

  • 这行告诉你源对象的绑定抛出错误(通常是DataContext

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

  • 而此行意味着它无法财产Text绑定在TextBoxDisplayMemberPath是使ItemTemplateTextBlock的快捷方式,与它的Text绑定到DisplayMemberPath属性)

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

因此,为了把它放在一起,这是告诉你,它试图绑定TextBox.Text{Binding Path=CategoryModel.CategoryList},但是TextBox背后的DataContextString类型,String没有一个所谓的财产CategoryModel

+0

是的,正是这个问题。感谢您的详细解释。 – mechanicum

+0

+1,顺便说一句当我有绑定错误,我认为XAML绑定是好的 - 然后datacontext是最错误的一个:)这就是为什么我总是先检查datacontext – blindmeis

+0

另一个问题,如果对象是'任务'可能缺少'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> 
相关问题