我有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?
1条答案
按热度按时间ql3eal8s1#
字符串