ObservableCollection binded to another ObservableCollection does not fire CollectionChanged

62 Views Asked by At

I have ControlA hosting ControlB, both of them have DependencyProperty of type ObservableCollection, the nested control's (ControlB) property is binded to parent (ControlA) property, if something is added to ControlA's collection - it's CollectionChanged is fired, but ControlB's is not, how can i make it fire? here is the minimal example: MainWindow.xaml:

<Window x:Class="WpfTest.MainWindow"
        ... >
    <Grid>
        <local:ControlA>
            <Rectangle/>
        </local:ControlA>
    </Grid>
</Window>

ControlA.xaml:

<UserControl x:Class="WpfTest.ControlA"
             ... 
             x:Name="ThisControlA">
    <Grid>
        <local:ControlB Items="{Binding Items, ElementName=ThisControlA}"/>
    </Grid>
</UserControl>

ControlA.xaml.cs:

[ContentProperty("Items")]
public partial class ControlA : UserControl
{
    public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<FrameworkElement>), typeof(ControlA));
    public ObservableCollection<FrameworkElement> Items
    {
        get { return (ObservableCollection<FrameworkElement>)GetValue(ItemsProperty); }
        set
        {
            if (Items != null)
                Items.CollectionChanged -= DockableHost_CollectionChanged;

            SetValue(ItemsProperty, value);

            Items.CollectionChanged += DockableHost_CollectionChanged;
        }
    }
    public ControlA()
    {
        InitializeComponent();

        DataContext = this;

        Items = new ObservableCollection<FrameworkElement>();
    }
    private void DockableHost_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Debug.WriteLine("This is firing.");
    }
}

ControlB.xaml:

<UserControl x:Class="WpfTest.ControlB"
             ... >
    <Grid>
            
    </Grid>
</UserControl>

ControlB.xaml.cs:

[ContentProperty("Items")]
public partial class ControlB : UserControl
{
    public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<FrameworkElement>), typeof(ControlB));
    public ObservableCollection<FrameworkElement> Items
    {
        get { return (ObservableCollection<FrameworkElement>)GetValue(ItemsProperty); }
        set
        {
            if (Items != null)
                Items.CollectionChanged -= DockableHost_CollectionChanged;

            SetValue(ItemsProperty, value);

            Items.CollectionChanged += DockableHost_CollectionChanged;
        }
    }

    public ControlB()
    {
        InitializeComponent();

        DataContext = this;

        Items = new ObservableCollection<FrameworkElement>();
    }
    private void DockableHost_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Debug.WriteLine("This is not firing.");
    }
}

any ideas how to make CollectionChanged fire in ControlB?

0

There are 0 best solutions below