简体   繁体   English

WPF DataGrid无法从ItemsSource绑定

[英]WPF DataGrid Unable to bind from ItemsSource

I've been looking at this for hours now but I cannot figure it out... I have successfully bound "Questions" to the ItemsSource. 我已经看了几个小时了,但是我无法弄清楚...我已经成功地将“问题”绑定到ItemsSource。 Questions is a ObservableCollection containing QuestionVM objects. Questions是一个包含QuestionVM对象的ObservableCollection。

For some reason the Text Column with the "Question" header cannot be bound to the Question property inside Questions. 由于某些原因,带有“问题”标题的文本列不能绑定到“问题”中的“问题”属性。

    <DataGrid ItemsSource="{Binding Questions}" AutoGenerateColumns="False" SelectedItem="{Binding SelectedQuestion, Mode=TwoWay}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Question" Binding="{Binding Question}" Width="*"/>
            <DataGridTemplateColumn Width="100">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Delete"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Width="100">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="Edit"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

Here is part of the QuestionVM class with the properties. 这是带有属性的QuestionVM类的一部分。

public class QuestionVM
{
    private Question _question;
    public string Question { get { return _question.Question1; } set { _question.Question1 = value; } }
    public string Category { get { return _question.Category; } set { _question.Category = value; } }
    public ObservableCollection<AnswerVM> Answers { get; set; }
}

I should be able to bind the Question property right? 我应该能够绑定Question属性吗? Why am I unable to do so? 为什么我不能这样做?

EDIT: Intellisense only shows the properties of the main data context and not the properties of the individual QuestionVMs inside the Questions ObservableCollection. 编辑:Intellisense仅显示主数据上下文的属性,而不显示Questions ObservableCollection内部的单个QuestionVM的属性。

        DataContext="{Binding ExistingQuestions, Source={StaticResource Locator}}

ExistingQuestions contains the properties Questions, SelectedQuestion and AddQuestion. ExistingQuestions包含属性Questions,SelectedQuestion和AddQuestion。 Those are the ones shown by Intellisense. 这些是Intellisense所显示的。

Pictures for further clarification: 图片进一步说明:

试图绑定..

As you can see those are not the properties of QuestionVM but the main datacontext. 如您所见,这些不是QuestionVM的属性,而是主要数据上下文。 It does not want to pick the properties from the ObservableCollection I've set as the ItemsSource. 它不想从我设置为ItemsSource的ObservableCollection中选择属性。

主数据上下文的属性。

我试图绑定在QuestionVM内部的此属性。

Updated Answer 更新的答案

If an object does not appear in Intellisense, it may be due to the namespace where the object is created. 如果对象未出现在Intellisense中,则可能是由于创建对象的名称空间所致。 Make sure that the objects are declared in the same namespace or try to add the namespace of the object Question to the view. 确保在同一名称空间中声明对象,或尝试将对象Question的名称空间添加到视图中。

Exemple of adding namespace to the view 向视图添加名称空间的示例

xmlns:helper="clr-namespace:Mynamespace.Myclass"

Also, it appears that the viewmodel has not implemented INotifyPropertyChanged interface which is used to notify the control's view when the binded property is updated. 此外,似乎该视图模型还没有实现INotifyPropertyChanged接口,该接口用于在绑定属性更新时通知控件的视图。

Cordialement Cordialement

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM