У меня есть ObservableCollection моего объекта класса вопросов викторины. У меня есть кнопка «Далее», и я хочу связать элементы управления окном с конкретным c объектом вопроса в этой коллекции. Привязка, кажется, работает (текстовое поле изначально заполнено правильно), но когда я нажимаю кнопку Далее, которая ДОЛЖНА заполнить мой объект currQuestion правильно, текстовое поле не обновляется, чтобы отобразить текущий вопрос, оно продолжает отображать исходный вопрос. Я максимально сократил xaml и code-behind, чтобы облегчить отладку, и да, я знаю, что должен использовать mvvm, поэтому дайте мне знать, если это является частью проблемы. Вот мой xaml
<Window x:Class="Quiz.QuizMaintWindow"
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:local="clr-namespace:Quiz"
mc:Ignorable="d"
Title="QuizMaintWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid Name="grd1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" MinWidth="100"/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Name="txtQuestion" Grid.Column="0" Grid.Row="0" Text="{Binding Question}"/>
<Button Name="btnNext" Grid.Column="0" Grid.Row="1" Content="Next" Margin="5" Click="btnNext_Click"/>
</Grid>
Вот мой код
public partial class QuizMaintWindow : Window
{
private clsQuestionAnswer qa; // this is the current question to be displayed
private ObservableCollection<clsQuestionAnswer> questions;
private int questionIndex = 0;
public QuizMaintWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
GetTopicQuestions();
grd1.DataContext = qa;
}
private void GetTopicQuestions()
{
questions = clsData.GetTopicQuestions(10);
qa = questions[questionIndex];
}
private void btnNext_Click(object sender, RoutedEventArgs e)
{
qa = questions[++questionIndex];
// MessageBox.Show(qa.Question);
}
}
и вот класс вопросов
public class clsQuestionAnswer: INotifyPropertyChanged
{
private int questionId;
private int topicId;
private string question;
private string answer;
private bool ignore;
public int QuestionId
{
get { return questionId; }
set
{
questionId = value;
NotifyPropertyChanged("QuestionId");
}
}
public int TopicId
{
get { return topicId; }
set
{
topicId = value;
NotifyPropertyChanged("TopicId");
}
}
public string Question
{
get { return question; }
set
{
question = value;
NotifyPropertyChanged("Question");
}
}
public string Answer
{
get { return answer; }
set
{
answer = value;
NotifyPropertyChanged("Answer");
}
}
public bool Ignore
{
get { return ignore; }
set
{
ignore = value;
NotifyPropertyChanged("Ignore");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}