2015-10-24 105 views
0

我在嘗試綁定到ObservableCollection時遇到下面列出的錯誤。它綁定到集合查找,因爲它返回正確數量的記錄,但似乎無法在集合中的對象上找到屬性(Name)。WPF ListView綁定找不到參數

感謝您的任何幫助。此外,如果這些錯誤消息中有詳細信息,我錯過了那些可以爲未來知道的很好的答案。我搜索了其他類似的帖子,但他們似乎更關心usercontrol本身的datacontext。

編輯:忘了補充我在DataContext設置在App.xaml中使用一個DataTemplate這樣

<DataTemplate DataType="{x:Type vm:StageProgressViewModel}"><v:StageProgressView /></DataTemplate> 

View.xaml

<UserControl x:Class="Program1.DatabaseConverter.Views.StageProgressView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-Program1.DatabaseConverter.Views" 
      mc:Ignorable="d"> 
    <Grid> 
     <ListView ItemsSource="{Binding ConverterStages}"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <WrapPanel> 
         <TextBlock Text="{Binding Name}"/> 
        </WrapPanel> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    </Grid> 
</UserControl> 

ViewModel.cs

using Program1.DatabaseConverter.MessageTypes; 
using GalaSoft.MvvmLight; 
using GalaSoft.MvvmLight.Messaging; 
using System.Collections.ObjectModel; 

namespace Program1.DatabaseConverter.ViewModels 
{ 
    public class StageProgressViewModel : ViewModelBase 
    { 
     public enum StageProgress 
     { 
      NotStarted, 
      InProgress, 
      Complete, 
      Failed 
     } 

     public class ConverterStage 
     { 
      public StageProgress Progress; 
      public string Name;    
     } 

     public ObservableCollection<ConverterStage> ConverterStages { get; set; } 

     public StageProgressViewModel() 
     { 
      ConverterStages = new ObservableCollection<ConverterStage>(); 
      ConverterStages.Add(new ConverterStage() { Progress = StageProgress.InProgress, Name = "Access Database" }); 
      ConverterStages.Add(new ConverterStage() { Progress = StageProgress.NotStarted, Name = "SQL Database" }); 

      ChangeStage(ConverterStages[0]);   
     } 

     private void ChangeStage(ConverterStage stage) 
     { 
      stage.Progress = StageProgress.InProgress; 

      var message = new ChangeStageDisplayedMessage(); 
      message.model = new SelectAccessDatabaseViewModel(); 

      Messenger.Default.Send<ChangeStageDisplayedMessage>(message); 
     } 
    } 
} 

錯誤

System.Windows.Data Error: 40 : BindingExpression path error: 'Name' property not found on 'object' ''ConverterStage' (HashCode=7686103)'. BindingExpression:Path=Name; DataItem='ConverterStage' (HashCode=7686103); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') 
System.Windows.Data Error: 40 : BindingExpression path error: 'Name' property not found on 'object' ''ConverterStage' (HashCode=16531454)'. BindingExpression:Path=Name; DataItem='ConverterStage' (HashCode=16531454); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') 
+0

添加干將財產屬性 – Sybren

回答

0

由於錯誤狀態沒有名爲 '名稱'

筆記/ setter方法到`ConverterStage`類屬性

public string Name { get; set; } 

與外地之間的差異

public string Name; 
+0

是否有可能綁定到不是屬性的東西?我以爲我看過類似的例子,或者可能綁定到結構(我最初使用的)。感謝您的答案,修復它! – Yaz

+0

不,你不能綁定到字段,[結構是邪惡的](http://stackoverflow.com/questions/441309/why-are-mutable-structs-vil),你不應該使用這些;班級應該是你的默認選擇。 – Rafal