如何检查列表的数据绑定何时完成?(wp7) [英] How can I check when the databinding of a list is complete? (wp7)
问题描述
我有一个数据透视控件,它的项目包含一个带有项目的列表框.当我滚动到下一个数据透视项时,数据绑定需要一些时间,我想知道数据绑定何时完成,因为我需要在列表框准备好出现后立即启用菜单栏.我在这里找不到可以帮助我的活动.我尝试了列表框的 Loaded 事件,但是虽然它适用于某些枢轴项目,但对于其他一些项目它不会触发!我也尝试过布局更新事件,但它被触发了太多次,对我没有帮助.
我能做什么?谢谢
为了在快速滚动透视项时确保良好的性能,您应该等待绑定透视项的内容,直到 SelectedIndex 更改.这样,当用户在 Pivot 项目之间快速滑动时,它不会尝试绑定;它只会在您停在 Pivot 项目上时绑定.
然后,您应该在 LayoutUpdated 事件的 Pivot 项中设置 ListBox 的 ItemsSource 属性.我使用以下扩展方法:
<预><代码>public static void InvokeOnLayoutUpdated(这个FrameworkElement元素,Action action){如果(元素==空){throw new ArgumentNullException("element");}否则如果(动作==空){throw new ArgumentNullException("action");}//创建一个事件处理程序,在调用//动作,然后将其附加到 LayoutUpdated 事件.EventHandler 处理程序 = null;处理程序 = (s, e) =>{element.LayoutUpdated -= 处理程序;行动();};element.LayoutUpdated += 处理程序;}所以你会有一些看起来像这样的代码:
<预><代码>枢轴.InvokeOnLayoutUpdate(() =>{Dispatcher.BeginInvoke(() =>{list.ItemsSource = 来源;ApplicationBar.IsMenuEnabled = true;});});I have a pivot control where its item contains a listbox with items. When I scroll to the next pivot item the data binding takes some time, and I want to know when the data binding is complete because I need to enable the menu bar, as soon as the listbox is ready to appear. I couldn't find an event that can help me here. I tried the Loaded event of the listbox but while it works for some pivot items, for some others it doesn't fire! I also tried the layout updated event but it is fired too many times and it can't help me.
What could i do? thank you
To ensure good performance when quickly scrolling through pivot items, you should wait to bind the contents of a pivot item until the SelectedIndex changes. That way it won't try to bind while the user is quickly swiping between Pivot items; it will only bind when you stop on a Pivot item.
You should then set the ItemsSource property of the ListBox in your Pivot item in the LayoutUpdated event. I use the following extension method:
public static void InvokeOnLayoutUpdated(this FrameworkElement element, Action action)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
else if (action == null)
{
throw new ArgumentNullException("action");
}
// Create an event handler that unhooks itself before calling the
// action and then attach it to the LayoutUpdated event.
EventHandler handler = null;
handler = (s, e) =>
{
element.LayoutUpdated -= handler;
action();
};
element.LayoutUpdated += handler;
}
So you would then have some code that looked something like this:
pivot.InvokeOnLayoutUpdate(() =>
{
Dispatcher.BeginInvoke(() =>
{
list.ItemsSource = source;
ApplicationBar.IsMenuEnabled = true;
});
});
这篇关于如何检查列表的数据绑定何时完成?(wp7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!