我有 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 触发吗?
[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.");
}
}