我如何获得itemsourcechanged事件?列表框

时间:2011-02-25 16:01:06

标签: silverlight silverlight-4.0

如何在列表框中获取itemsourcechangedevent?

例如。 itemsource从null更改为ListA,然后更改为ListB

我知道没有这样的事件。但有没有解决方法?

提前致谢:)

2 个答案:

答案 0 :(得分:7)

常用(已回答)方法是使用Blend SDK中的PropertyChangedTrigger。但是我不建议使用其他SDK,除非有明确的迹象表明SDK已经在使用。

我现在假设它在代码隐藏中你想要监听“ItemsSourceChanged”事件。您可以使用的一种技术是在DependencyProperty中创建UserControl并将其绑定到您要收听的控件的ItemsSource。

private static readonly DependencyProperty ItemsSourceWatcherProperty = 
    DependencyProperty.Register(
       "ItemsSourceWatcher",
       typeof(object),
       typeof(YourPageClass),
       new PropertyMetadata(null, OnItemsSourceWatcherPropertyChanged));

private static void OnItemsSourceWatcherPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    YourPageClass source = d As YourPageClass;
    if (source != null)
        source.OnItemsSourceWatcherPropertyChanged();  
}

private void OnItemsSourceWatcherPropertyChanged()
{
    // Your code here.
}

现在假设您的ListBox名称为“myListBox”,您可以设置观看: -

Binding b = new Binding("ItemsSource") { Source = myListBox };
SetBinding(ItemsSourceWatcherProperty, b);

答案 1 :(得分:1)

ItemsSourceChanged中没有Silverlight个事件。

但是,有一种解决方法。使用this article中提到的RegisterForNotification()方法为ListBox ItemsSource 属性注册属性值更改回调。

相关问题