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:
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.
Question
being your primary backing type AND an important public property of your class is fairly confusing. ConsiderQuestionDTO
for the type? – zzxyzBinding = {Binding Questions.Question}
. – fusselQuestion
is not displayed, or the value of it isn't updated, if you change it in ViewModel? – Rekshino