Hallo,
I am making a quiz game with entityframework and MVVM light. Currently i am working on a part where you can create questions and answers to put into a quiz.
What i'm trying to do is when i click on one of the questions i want to see the answers that are linked to it in the datagrid below. When i click the question an ObservableCollection gets filled with the answers, but it is not showing up in anyway in the datagrid even though the collection gets filled properly.
XAML:
<Window x:Class="Kwisspel.View.QuestionManagement" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:mvvm="http://www.galasoft.ch/mvvmlight" xmlns:local="clr-namespace:Kwisspel.View" mc:Ignorable="d" DataContext="{Binding QuestionManagement, Source={StaticResource Locator}}" Title="QuestionManagement" Height="450" Width="800"><StackPanel><StackPanel Orientation="Horizontal" Margin="10,10,0,10"><TextBlock Margin="0,0,10,0">Nieuw vraag:</TextBlock><TextBox x:Name="QuestionText" Width="500" Margin="0,0,10,0"></TextBox><Button Command="{Binding AddQuestions}" CommandParameter="{Binding Text, ElementName=QuestionText}">Toevoegen</Button></StackPanel><DataGrid x:Name="QuestionsGrid" Margin="0,0,0,5" ItemsSource="{Binding Questions, Mode=TwoWay}" Height="150" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto"><DataGrid.Columns></DataGrid.Columns><i:Interaction.Triggers><i:EventTrigger EventName="SelectionChanged"><mvvm:EventToCommand Command="{Binding SelectedQuestion, Mode=TwoWay}" CommandParameter="{Binding ElementName=QuestionsGrid,Path=SelectedItem}" /></i:EventTrigger></i:Interaction.Triggers></DataGrid><StackPanel Margin="10,5,0,10" Orientation="Horizontal"><TextBlock Margin="0,0,10,0">Nieuw antwoord:</TextBlock><TextBox x:Name="AnswerText" Width="500" Margin="0,0,10,0"></TextBox><Button Command="{Binding AddAnswers}" CommandParameter="{Binding Text, ElementName=AnswerText}" x:Name="AnswerButton">Toevoegen</Button></StackPanel><DataGrid x:Name="AnswerDatagrid" ItemsSource="{Binding Answers, Mode=TwoWay}" Height="150" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Auto"></DataGrid></StackPanel></Window>
ViewModel:
public ICommand SelectedQuestion { get; set; } public ObservableCollection<QuestionViewModel> Questions { get; set; } public ObservableCollection<AnswerViewModel> Answers { get; set; } public List<AnswerViewModel> ModifiedItems { get; set; } public QuestionManagementViewModel() { SelectedQuestion = new RelayCommand<QuestionViewModel>(getInfo); Answers = new ObservableCollection<AnswerViewModel>(); using (var context = new KwisspelEntities()) { var questions = context.Question .ToList() .Select(q => new QuestionViewModel(q)); Questions = new ObservableCollection<QuestionViewModel>(questions); } } public void getInfo(QuestionViewModel question) { if (question != null) { if (Answers != null) { Answers.Clear(); } using (var context = new KwisspelEntities()) { var answers = context.Answer .Where(a => a.Question_Id == question.id) .ToList() .Select(a => new AnswerViewModel(a)); Answers = new ObservableCollection<AnswerViewModel>(answers); } } }
Thank you in advance