我是ObservableCollections的新手,但已經構建了一些我確信應該可以工作的代碼。不幸的是,它沒有。唯一沒有發生的事情是,我的GUI沒有被更新。我知道後面的值正在更新(使用調試器進行檢查)。Silverlight UI不更新 - ObservableCollection被重新使用
我在做什麼錯?
與我的XAML的文字塊的樣本這裏:我的代碼
<TextBlock Name="tbCallsOpen" Text="{Binding IndicatorValue}" />
。由此樣本背後:
public partial class CurrentCalls : UserControl
{
Microsoft.SharePoint.Client.ListItemCollection spListItems;
ObservableCollection<CurrentCallIndicator> CallIndicators = new ObservableCollection<CurrentCallIndicator>();
public CurrentCalls()
{
InitializeComponent();
DispatcherTimer dispatchTimer = new DispatcherTimer();
dispatchTimer.Interval = new TimeSpan(0, 0, 20);
dispatchTimer.Tick += new EventHandler(BindData);
dispatchTimer.Start();
}
private void BindData(object sender, EventArgs args)
{
//splistitems is a sharepoint list. Data is being retrieved succesfully, no issues here.
foreach (var item in spListItems)
{
//My custom class which implements INotifyPropertyChanged
CurrentCallIndicator indicator = new CurrentCallIndicator();
indicator.IndicatorValue = item["MyValueColumn"];
//Adding to ObservableCollection
CallIndicators.Add(indicator);
}
//Setting Datacontext of a normal TextBlock
tbCallsOpen.DataContext = CallIndicators.First(z => z.IndicatorName == "somevalue");
}
}
好吧,我現在也添加綁定到ObservableCollection的CollectionChanged事件,仍然沒有喜悅..值更新,但控件不.. – Fox
刪除所有不需要的代碼,如DispatchTimer,看看它是否工作。需要縮小範圍。您也可以檢查輸出窗口。 –