views:

497

answers:

1

Hi,

I create a UserControl (TableWithFilter.xaml) with a dependency property (source). The UserControl is a Table with a source property for the different items. I created the XAML and set the source property via the XAML Binding. So far so good.

But if the value of the dependency property is changed, the defined callback method is not called. Therefore I cannot update the entries in my table. Has anyone an idea why the callback method is not called?

Here is the definition of my property in the class "TableWithFilter":

Public Shared ReadOnly SourceProperty As DependencyProperty = _
        DependencyProperty.Register("Source", GetType(List(Of TableViewItem)), GetType(TableWithFilter), _
                                    New FrameworkPropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf TableWithFilter.ChangeSource)))

and the Callback method:

 Private Shared Sub ChangeSource(ByVal source As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim table As TableWithFilter = source
        table.Source = e.NewValue
    End Sub

and here the XAML:

<Border Grid.Row="1" Grid.Column="1" BorderBrush="{StaticResource ElementBorder}" BorderThickness="1">
    <local:TableWithFilter x:Name="SearchResultTable" Source="{Binding Source={StaticResource contentFacade}, Path=ContentList}" />    
</Border>

If the attribute "ContentList" is changed I expet that the "ChangeSource" method in the TableWithFilder class is called. But this is not the case. After I changed the ContentList attribute, I Raise the following Event:

RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("ContentList"))

Thx for any ideas.

+1  A: 

There's a difference between the entire property changing (i.e. a new reference to a collection being specified) vs. the contents of the list itself changing. Collections have their own change notification interface called INotifyCollectionChanged. So what you would need to do is listen for the property change you have now and, inside of that, hook the INotifyCollectionChanged events yourself on the new collection instance and then respond to those.

Drew Marsh
You are right. I found the solution. It was not needed to add an own event handlet to the collection, but I had to change the data typ from List(Of..) to ObservableCollection. With following definition everythings works fine: Public Shared ReadOnly SourceProperty As DependencyProperty = _ DependencyProperty.Register("Source", GetType(ObservableCollection(Of TableViewItem)), GetType(TableWithFilter), _ New PropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf TableWithFilter.ChangeSource)))Thanks a lot.
Chameleon