如何通过ViewModel将observableCollection绑定到DataGrid?

时间:2017-07-23 04:01:45

标签: c# wpf datagridview binding observablecollection

我在第一页上列出了我的内容。一旦用户双击该listVIew中的一个项目,我就应该显示一个GridView,他们可以在其中添加/更新/删除项目。我的代码一直工作,直到双击。我还看到我的GridView上的正确绑定,并从数据库中获取正确的项目数,但UI没有显示任何内容。每个项目的绑定错误为40.请帮助!

当我窥探QuestionCode时,这是错误:

System.Windows.Data Error: 40 : BindingExpression path error: 'QuestionCode' property not found on 'object' ''Char' (HashCode=4390979)'. BindingExpression:Path=QuestionCode; DataItem='Char' (HashCode=4390979); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

这是我的xaml:

<DockPanel>
        <DataGrid DockPanel.Dock="Top" x:Name="gridQuestionList" ItemsSource="{Binding Source=CheckListQuestions}" IsReadOnly="False">
            <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Question Code" MinWidth="70">
                        <DataGridTemplateColumn.HeaderStyle>
                            <Style TargetType="{x:Type DataGridColumnHeader}">
                                <Setter Property="HorizontalAlignment" Value="Stretch"/>
                                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                            </Style>
                        </DataGridTemplateColumn.HeaderStyle>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock VerticalAlignment="Center" Padding="3,0,8,0" Text="{Binding QuestionCode, ValidatesOnDataErrors=true, UpdateSourceTrigger=PropertyChanged}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <TextBox VerticalAlignment="Center" Text="{Binding QuestionCode, ValidatesOnDataErrors=true, UpdateSourceTrigger=PropertyChanged}" Foreground="Black" IsReadOnly="False"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>

这是我背后的代码:

public partial class CheckListQuestionWindow : Window
{
    private string _checklistCode = string.Empty;
    private CheckListQuestionViewModel _viewModel;
    public CheckListQuestionWindow()
    {
        InitializeComponent();
    }

    public CheckListQuestionWindow(string checkListCode) : this()
    {
        _checklistCode = checkListCode;
        if (_viewModel == null)
            _viewModel = new CheckListQuestionViewModel(_checklistCode);

        DataContext = _viewModel;
    }
}

这是我的ViewModel:

ObservableCollection<ChecklistQuestion> CheckListQuestions { get; set; }

    public CheckListQuestionViewModel(string code)
    {
        var list = ChecklistQuestionList.GetChecklistQuestionList(code);
        CheckListQuestions = list;
       //here i'm getting correct count of checklistquestions but there's no data
    }

这是我的模特:

public string QuestionCode
{
    get { return GetProperty(QuestionCodeProperty); }
    set { SetProperty(QuestionCodeProperty, value); }
}

1 个答案:

答案 0 :(得分:0)

替换

ItemsSource="{Binding Source=CheckListQuestions}" 

通过

ItemsSource="{Binding Path=CheckListQuestions}" 

或只是

ItemsSource="{Binding CheckListQuestions}" 

使用Source=CheckListQuestions时,ItemsSource属性获取字符串&#34; CheckListQuestions&#34;作为值,IEnumerable<char>。因此错误'QuestionCode' property not found on 'object' ''Char'

还要确保CheckListQuestions是公共财产,即

public ObservableCollection<ChecklistQuestion> CheckListQuestions { get; set; }

而不是

ObservableCollection<ChecklistQuestion> CheckListQuestions { get; set; }