WPF HierarchicalDataTemplate does not update ItemsSource property on property

Here are a few XAML

<HierarchicalDataTemplate DataType="{x:Type data:FolderEntity}" 
  ItemsSource="{Binding Path=FolderEntities,UpdateSourceTrigger=PropertyChanged}">
  <Label Content="{Binding FolderName}"/>
</HierarchicalDataTemplate>
<TreeView/>

: FolderEntity is a LINQ to SQL data class that implements the INotifyPropertyChanging and INotifyPropertyChanged interfaces.

My problem is that when I change the FolderEntities property, the binding does not update. If I change the FolderName property, the node tree corresponding to this element will change, but the collection, which is FolderEntities, simply will not.

I think WPF is checking to see if the link to the collection has changed, or should the ItemsSource be an ObservableCollection`1 for this?

Any input on this subject is welcome.

+3
source share
3 answers

, (FolderEntities) ObservableCollection<T> HierarchicalDataTemplate, . , , INotifyCollectionChanged.

+3

, !

public abstract class ObservableHierarchy<T>
{
    public T Current { get; set; }

    public ObservableCollection<ObservableHierarchy<T>> Children { get; set; }

    public ObservableHierarchy( T current, Func<T, IEnumerable<T>> expand )
    {
        this.Current = current;
        this.Children = new ObservableCollection<ObservableHierarchy<T>>();
        foreach ( var item in expand( current ) )
        {
            Children.Add( Create( item ) );
        }
    }

    protected abstract ObservableHierarchy<T> Create( T item );
}

, , .

public class ObservableFolderHierarchy:
    ObservableHierarchy<FolderEntity>
{
    public ObservableFolderHierarchy( FolderEntity root )
        : base( root, x => x.FolderEntities )
    {
    }

    protected override ObservableHierarchy<FolderEntity> Create( FolderEntity item )
    {
        return new ObservableFolderHierarchy( item );
    }
}

XAML, ! Current TreeView .

<HierarchicalDataTemplate DataType="{x:Type ui:ObservableFolderHierarchy}"
 ItemsSource="{Binding Children}">
 <StackPanel Orientation="Horizontal">
  <Image Source="/UI/Resources/folder.png" Width="16" Height="16"/>
  <TextBlock Text="{Binding Current.FolderName}"/>
 </StackPanel>
</HierarchicalDataTemplate>
+2

. , ObservableCollection.

What you were attached to was a property, and the PropertyChanged property (from INotifyPropertyChanged) was called only when the property was set, for example. FolderEntities = aNewValue;the binding mechanism was not aware of any changes that occurred in the collection.

0
source

All Articles