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

TCM*_*TCM 5 silverlight silverlight-4.0

如何在列表框中获取itemsourcechangedevent?

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

我知道没有这样的事件.但这有什么解决方法吗?

提前致谢 :)

Ant*_*nes 7

常用(回答)方法是使用PropertyChangedTriggerBlend SDK中的.但是我不建议使用其他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.
}
Run Code Online (Sandbox Code Playgroud)

现在假设您ListBox的名字为"myListBox",您可以设置观看: -

Binding b = new Binding("ItemsSource") { Source = myListBox };
SetBinding(ItemsSourceWatcherProperty, b);
Run Code Online (Sandbox Code Playgroud)