WPF ListView绑定没有找到参数

本文关键字:参数 ListView 绑定 WPF | 更新日期: 2023-09-27 18:19:01

当尝试绑定到ObservableCollection时,我得到了下面列出的错误。它绑定到集合查找,因为它返回了正确数量的记录,但似乎无法找到集合中对象的属性(Name)。

谢谢你的帮助。此外,如果在这些错误消息中有一个细节,我错过了,指向回答,这将是很好的知道未来。我搜索了其他类似的帖子,但他们似乎更多的是关于usercontrol本身的数据上下文。

编辑:忘记添加我有数据上下文集从使用数据模板在app.xaml如

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

WPF ListView绑定没有找到参数

错误提示没有名为" Name "的属性

注意属性

之间的区别
public string Name { get; set; }

字段
public string Name;