WPF ListView绑定未查找参数

时间:2015-10-24 15:12:00

标签: c# wpf xaml

我在尝试绑定到ObservableCollection时遇到下面列出的错误。它与集合的绑定发现它正在返回正确数量的记录,但似乎无法在集合中的对象上找到属性(Name)。

感谢您的帮助。此外,如果在这些错误消息中有详细信息,我会错过指出要回答的信息,以便将来知道。我已经搜索了其他类似的帖子,但它们似乎更多的是关于用户控件本身的datacontext。

编辑:忘记添加我在app.xaml中使用datatemplate设置了datacontext

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

1 个答案:

答案 0 :(得分:0)

由于错误状态,因此没有名为“名称”的属性

注意属性

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

和字段

public string Name;