Silverlight UI 未更新 - ObservableCollection 正在重新实例化

发布于 2024-12-20 08:34:30 字数 1520 浏览 1 评论 0原文

我对 ObservableCollections 还很陌生,但已经构建了一些我确信应该可以工作的代码。不幸的是事实并非如此。唯一没有发生的事情是我的 GUI 没有更新。我知道这些值正在后面更新(使用调试器检查)。

我做错了什么?

这里是我的 Textblock 的 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");
    }
}

I'm pretty new to ObservableCollections, but have built some code which I'm sure should work. Unfortunately it doesn't. The only thing that is not happening, is my GUI is not being updated. I know the values are being updated in the back (Checked using Debugger).

What am I doing wrong?

Here with a sample of my XAML for the Textblock:

<TextBlock Name="tbCallsOpen" Text="{Binding IndicatorValue}" />

Herewith sample of my code behind:

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");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放肆 2024-12-27 08:34:30

您很可能假设对集合中基础项目的更改将引发 CollectionChanged 事件;但这不是 ObservableCollection 的工作原理。

如果您想要这种行为,您需要滚动自己的实现,并且当集合中的项目内触发 PropertyChanged 事件时,您需要触发 CollectionChanged 事件。

You are most likely assuming that changes to the underlying items in the collection will raise the CollectionChanged event; however that is not how the ObservableCollection<T> works.

If you wanted this behavior you would need to roll your own implmentation and when a PropertyChanged event is fired within an item within your collection, you would then need to fire the CollectionChanged event.

小瓶盖 2024-12-27 08:34:30

乍一看,你的代码对我来说或多或少是正确的 - 尽管我不认为你需要使用 ObservableCollection<>获得您似乎期望的结果:一个简单的 List<>会工作得很好。

如果调试器告诉您 DataContext 已正确更新为预期项目,则最有可能的问题是您的绑定定义方式存在问题。如果您没有在调试窗口中看到任何绑定错误报告,那么我会查看 Bea Stollnitz 的文章 调试绑定。最具体地说,我经常使用她建议的“DebugValueConverter”技术,例如:

/// <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;
    }
}

然后在转换器中设置一个断点,并观察发生了什么。这是一种破解和拼凑,但在我们全部使用 SL5(内置绑定调试)之前,这是您最好的选择。

Your code looks more-or-less correct to me, at first blush - though I wouldn't expect that you'd need to use an ObservableCollection<> to get the results you seem to be expecting: a simple List<> would work just fine.

If the debugger tells you that the DataContext is being updated correctly to the expected item, then the most likely issue is that there's a problem with how your binding is defined. If you're not seeing any binding errors reported in your debug window, then I'd look into Bea Stollnitz' article on debugging bindings. Most specifically, I often use the technique she suggests of a "DebugValueConverter", e.g.:

/// <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;
    }
}

And then set a breakpoint in your converter, and watch what's happening. It's a hack and a kludge, but until we're all on SL5 (which has binding debugging built in), it's your best bet.

寒江雪… 2024-12-27 08:34:30

好的,已排序。我自己解决了这个问题。因为我在循环中更新值,所以 ObservableCollection 没有正确更新。我在数据绑定方法开始时所做的就是清除集合:CallIndicators.Clear();

Ok, Sorted. I fixed the issue myself. Because I was updating the values in a loop, the ObservableCollection wasn't being updated properly. All I did in the beginning of the databinding method, was to Clear the collection : CallIndicators.Clear();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文