1
votes

I've been looking at this for hours now but I cannot figure it out... I have successfully bound "Questions" to the ItemsSource. Questions is a ObservableCollection containing QuestionVM objects.

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.

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? 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.

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

ExistingQuestions contains the properties Questions, SelectedQuestion and AddQuestion. Those are the ones shown by Intellisense.

Pictures for further clarification:

Attempting to bind..

As you can see those are not the properties of QuestionVM but the main datacontext. It does not want to pick the properties from the ObservableCollection I've set as the ItemsSource.

Properties of main data context.

I am trying to bind this property which is inside the QuestionVM.

1
I don't think this is your issue, but Question being your primary backing type AND an important public property of your class is fairly confusing. Consider QuestionDTO for the type?zzxyz
Is Question a member of Questions? In that case the binding should be Binding = {Binding Questions.Question}.fussel
@zzxyz I thought so too. Will most likely change it.BobiSad
@fussel Question is a property inside the QuestionVM. The ItemsSource is bound to a ObservableCollection of QuestionVMs.BobiSad
What do you mean with unable to bind? The Question is not displayed, or the value of it isn't updated, if you change it in ViewModel?Rekshino

1 Answers

-2
votes

Updated Answer

If an object does not appear in Intellisense, it may be due to the namespace where the object is created. Make sure that the objects are declared in the same namespace or try to add the namespace of the object Question to the view.

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.

Cordialement