ObservableCollection 绑定到另一个 ObservableCollection 不会触发 CollectionChanged

问题描述 投票:0回答:1

我有 ControlA 托管 ControlB,它们都有 ObservableCollection 类型的 DependencyProperty,嵌套控件的 (ControlB) 属性绑定到父 (ControlA) 属性,如果将某些内容添加到 ControlA 的集合中 - 它会触发 CollectionChanged,但 ControlB 不会,我怎样才能让它着火?这是最小的例子: 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.");
    }
}

有什么想法可以让 ControlB 中的 CollectionChanged 触发吗?

c# wpf xaml observablecollection dependency-properties
1个回答
0
投票
[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 { SetValue(ItemsProperty, value); }
    }

    public ControlA()
    {
        InitializeComponent();

        DataContext = this;

        Items = new ObservableCollection<FrameworkElement>();
        Items.CollectionChanged += DockableHost_CollectionChanged;
    }

    private void DockableHost_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Debug.WriteLine("This is firing.");
    }
}

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 { SetValue(ItemsProperty, value); }
    }

    public ControlB()
    {
        InitializeComponent();

        DataContext = this;

        Items = new ObservableCollection<FrameworkElement>();
        Items.CollectionChanged += DockableHost_CollectionChanged;
    }

    private void DockableHost_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Debug.WriteLine("This is firing.");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.