开发者

How do i get the itemsourcechanged event? Listbox

开发者 https://www.devze.com 2023-02-13 16:26 出处:网络
How can i get the items开发者_运维百科ourcechangedevent in listbox? For eg. the itemsource changes from null to ListA then to ListB

How can i get the items开发者_运维百科ourcechangedevent in listbox?

For eg. the itemsource changes from null to ListA then to ListB

I know there is no such event. But is there any workaround for this?

Thanks in advance :)


A commonly used (answered) approach is use the PropertyChangedTrigger from the Blend SDK. However I don't like recommending the use of other SDKs unless there is a clear indication the SDK is already in use.

I'll assume for the moment that its in code-behind that you want listen for a "ItemsSourceChanged" event. A technique you can use is to create a DependencyProperty in your UserControl and bind it to the ItemsSource of the control you want to listen to.

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.
}

Now given that your ListBox has a name "myListBox" you can set up watching with:-

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


There is no ItemsSourceChanged event in Silverlight.

But, there is a workaround. Use RegisterForNotification() method mentioned in this article to register a property value change callback for ListBox's ItemsSource property.

0

精彩评论

暂无评论...
验证码 换一张
取 消