c# - 转到 observablecollection 中的下一个对象时,窗口控件不更新
问题描述
我有一个我的测验问题类对象的 ObservableCollection。我有“下一步”按钮,并希望将窗口控件绑定到该集合中的特定问题对象。绑定似乎有效(最初正确填充了文本框),但是当我单击下一步时,它确实正确填充了我的 currQuestion 对象,文本框不会更新以显示当前问题,它会继续显示原始问题。我已经尽可能地剥离了 xaml 和代码隐藏,以使其更易于调试,是的,我知道我应该使用 mvvm,所以让我知道这是否是问题的一部分。这是我的xml
<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));
}
}
}
解决方案
您需要通知页面或网格基础数据已更改。所以在按钮代码中添加:
grd1.DataContext = qa = questions[++questionIndex];
不,该代码不是错字,可以对最终值进行多次分配。
推荐阅读
- java - 带有 RMI 的 Java 客户端服务器
- r - 如何在R中的excel中将日期和时间转换为持续时间列
- sql - 提高查询性能,添加 where 子句网格查询停止
- php - 使用 File_Put_Contents 和 File_get_Contents 上传 50,000 张图像的最佳方法
- python-3.x - 读取 Dukascopy .bi5 ohlc 刻度文件
- javascript - AngularJS将递归JSON解析为对象数组
- ios - 为 ios 运行 cordova build 时出错:IONIC 1
- xslt - 根据子节点的条件和存在复制和过滤 XML
- axios - 使用 React 钩子和 Context API 更新全局状态并从多个端点获取/提供数据的最佳实践
- laravel - 为什么 Laravel Mix 会删除 CSS @import url()