【发布时间】:2014-08-24 14:09:38
【问题描述】:
我有一个非常小的应用程序,其中有一个笑话列表,一切正常,但每次我想在 TextBlock 中显示信息时都必须重新设置 DataContext。 SAME页面的TextBlock发生变化,我只想更改笑话ID以获得新的笑话。
private IList<Jokes> data;
Array variable;
private int count;
// Constructor
// ObservableCollection<Jokes> = new ObservableCollectoin<Jokes>;
public MainPage()
{
InitializeComponent();
JokeRepository countryRepository = new XmlJokeRepository();
data = countryRepository.GetCountryList().ToList<Jokes>();
DataContext = countryRepository.GetCountryById(3);
}
private void nextIconClick(object sender, EventArgs e)
{
Random rand = new Random();
int holder = rand.Next(1, count);
DataContext = data.FirstOrDefault(c => c.ID == holder);
}
在我的 XAML 中,我的 {Binding Description} 属性工作正常,但我真的觉得这是多余的和无用的。尽管我仍然使用相同的变量/页面,但是否有另一种方法可以解决这个问题而不必每次都重置 DataContext?
【问题讨论】:
标签: xaml windows-phone