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

pdkcd3nj  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(112)

我有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?

ql3eal8s

ql3eal8s1#

public partial class ControlB : UserControl
{
    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.");
    }

    // Other code...
}

字符串

相关问题