Silverlight UI未更新-正在重新实例化ObservableCollection
我对ObservableCollections非常陌生,但我已经构建了一些代码,我相信这些代码应该可以工作。不幸的是,事实并非如此。唯一没有发生的事情是我的GUI没有被更新。我知道后面的值正在更新(使用调试器检查) 我做错了什么 下面是我的用于Textblock的XAML示例:Silverlight UI未更新-正在重新实例化ObservableCollection,silverlight,data-binding,observablecollection,Silverlight,Data Binding,Observablecollection,我对ObservableCollections非常陌生,但我已经构建了一些代码,我相信这些代码应该可以工作。不幸的是,事实并非如此。唯一没有发生的事情是我的GUI没有被更新。我知道后面的值正在更新(使用调试器检查) 我做错了什么 下面是我的用于Textblock的XAML示例: <TextBlock Name="tbCallsOpen" Text="{Binding IndicatorValue}" /> 下面是我的代码示例: public partial class Curr
<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");
}
}
public部分类CurrentCalls:UserControl
{
Microsoft.SharePoint.Client.ListItemCollection spListItems;
ObservableCollection CallIndicators=新的ObservableCollection();
公共电话()
{
初始化组件();
Dispatchermer DispatcheTimer=新Dispatchermer();
dispatchTimer.Interval=新的时间跨度(0,0,20);
dispatchTimer.Tick+=新的EventHandler(BindData);
dispatchTimer.Start();
}
私有void BindData(对象发送方、事件args args)
{
//splistitems是sharepoint列表。正在成功检索数据,此处没有问题。
foreach(spListItems中的var项)
{
//实现INotifyPropertyChanged的自定义类
CurrentCallIndicator指示器=新的CurrentCallIndicator();
indicator.IndicatorValue=项目[“MyValueColumn”];
//添加到ObservableCollection
Callindicator.Add(指示器);
}
//设置普通文本块的Datacontext
tbCallsOpen.DataContext=CallIndicators.First(z=>z.IndicatorName==“somevalue”);
}
}
您很可能假设对集合中基础项的更改将引发CollectionChanged
事件;然而,observeableCollection
不是这样工作的
如果您想要此行为,则需要滚动您自己的实现,并且当在集合中的某个项目中触发
PropertyChanged
事件时,然后您需要触发CollectionChanged
事件。您很可能假设对集合中的基础项的更改将引发CollectionChanged
事件;然而,observeableCollection
不是这样工作的
如果您想要这种行为,您需要滚动您自己的实现,并且当在集合中的某个项目中触发
PropertyChanged
事件时,您需要触发CollectionChanged
事件。您的代码在我看来或多或少是正确的,乍一看——虽然我并不期望您需要使用ObservableCollection来获得您似乎期望的结果:一个简单的列表就可以了
如果调试器告诉您DataContext被正确更新为预期项,那么最可能的问题是绑定的定义方式有问题。如果您在调试窗口中没有看到报告的任何绑定错误,那么我将查看Bea Stollnitz的文章。最具体地说,我经常使用她建议的“DebugValueConverter”技术,例如:
//
///帮助调试绑定。像这样使用:Content=“{Binding PropertyName,Converter={StaticResource debugConverter}”
///
公共类DebugConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回值;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回值;
}
}
然后在转换器中设置一个断点,并观察发生了什么。这是一个难题和难题,但在我们都使用SL5(内置绑定调试)之前,这是您的最佳选择。乍一看,您的代码在我看来或多或少是正确的-尽管我不希望您需要使用ObservableCollection来获得您似乎期望的结果:一个简单的列表就可以了 如果调试器告诉您DataContext被正确更新为预期项,那么最可能的问题是绑定的定义方式有问题。如果您在调试窗口中没有看到报告的任何绑定错误,那么我将查看Bea Stollnitz的文章。最具体地说,我经常使用她建议的“DebugValueConverter”技术,例如:
//
///帮助调试绑定。像这样使用:Content=“{Binding PropertyName,Converter={StaticResource debugConverter}”
///
公共类DebugConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回值;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回值;
}
}
然后在转换器中设置一个断点,并观察发生了什么。这是一个难题,但在我们都使用SL5(内置绑定调试)之前,它是您的最佳选择。好的,分类。我自己解决了这个问题。因为我正在更新循环中的值,所以ObservableCollection没有正确更新。在数据绑定方法开始时,我所做的只是清除集合:CallIndicators.Clear() 好的,已排序。我自己解决了这个问题。因为我正在更新循环中的值,所以ObservableCollection没有正确更新。在数据绑定方法开始时,我所做的只是清除集合:CallIndicators.Clear() 好的,我现在已经在ObservableCollection上添加了CollectionChanged事件的绑定,但仍然没有乐趣。。值已更新,但控件未更新。请删除所有不需要的代码,如DispatchTimer,然后查看它是否工作。需要缩小范围。您还可以检查输出窗口。好的,我现在还添加了绑定到ObservaleCollection上的CollectionChanged事件,仍然是n
/// <summary>
/// Helps to debug bindings. Use like this: Content="{Binding PropertyName, Converter={StaticResource debugConverter}}"
/// </summary>
public class DebugConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}