我在嘗試綁定到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')
添加干將財產屬性 – Sybren