在这一行:
<ItemsControl ItemsSource="{Binding UCItems}" ... />
必须是 RelativeSource 和 FindAncestor,因为 UCItems 位于 UserControl:
UserControl
<ItemsControl ItemsSource="{Binding Path=UCItems,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />
I cannot understand why OnUCItemsSourceChanged is not called?
如果你添加RelativeSource 构造,那么OnUCItemsSourceChanged 至少会导致一次,因为PropertyChangedCallback 每次都会触发然后你为依赖属性设置新值:
表示依赖属性的有效属性值发生变化时调用的回调。
因为你曾经在这里设置了依赖属性的值:
<uc:UserControl1 UCItemsSource="{Binding Path=WindowCollection}" />
I expect UserControl1.OnUCItemsSourceChanged to be called when I add items to WindowCollection.
这是一个ObservableCollection<T>.CollectionChanged 事件,其中包含对集合执行的操作的枚举:
在添加、删除、更改、移动项目或刷新整个列表时发生。
对于您的情况,它将是这样的:
Version with CollectionChanged
MainWindow
public partial class MainWindow : Window
{
public ObservableCollection<long> WindowCollection { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
WindowCollection = new ObservableCollection<long>();
WindowCollection.Add(1);
WindowCollection.Add(2);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
WindowCollection.Add(3);
WindowCollection.Add(4);
}
}
UserControl
public partial class UserControl1 : UserControl
{
#region Public Section
public ObservableCollection<long> UCItems { get; set; }
public static UserControl1 control;
#endregion
public UserControl1()
{
InitializeComponent();
UCItems = new ObservableCollection<long>();
}
#region UCItemsSource Property
public static readonly DependencyProperty UCItemsSourceProperty = DependencyProperty.Register("UCItemsSource",
typeof(IEnumerable),
typeof(UserControl1),
new PropertyMetadata(null, new PropertyChangedCallback(OnUCItemsSourceChanged)));
public IEnumerable UCItemsSource
{
get { return (IEnumerable)GetValue(UCItemsSourceProperty); }
set { SetValue(UCItemsSourceProperty, value); }
}
#endregion
private static void OnUCItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
control = d as UserControl1;
var items = e.NewValue as ObservableCollection<long>;
items.CollectionChanged += new NotifyCollectionChangedEventHandler(CollectionChanged);
AddItem(control, items);
}
private static void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
var items = sender as ObservableCollection<long>;
control.UCItems.Clear();
if (e.Action == NotifyCollectionChangedAction.Add)
{
AddItem(control, items);
}
}
private static void AddItem(UserControl1 userControl, ObservableCollection<long> collection)
{
if (collection.Count > 0)
{
foreach (var item in collection)
{
userControl.UCItems.Add(item);
}
}
}
}
此项目可在此link
Alternative version
这个版本更简单,更正确。这里我们只引用了包含集合的UCItemsSource 属性,这里也是RelativeSource justified:
UserControl
XAML
<Grid>
<ItemsControl ItemsSource="{Binding Path=UCItemsSource,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type UserControl}}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Code-behind
public partial class UserControl1 : UserControl
{
#region Public Section
public ObservableCollection<long> UCItems { get; set; }
#endregion
public UserControl1()
{
InitializeComponent();
UCItems = new ObservableCollection<long>();
}
#region UCItemsSource Property
public static readonly DependencyProperty UCItemsSourceProperty = DependencyProperty.Register("UCItemsSource",
typeof(IEnumerable),
typeof(UserControl1));
public IEnumerable UCItemsSource
{
get { return (IEnumerable)GetValue(UCItemsSourceProperty); }
set { SetValue(UCItemsSourceProperty, value); }
}
#endregion
}