Sử dụng một thuộc tính phụ thuộc ItemsSource trong CustomControl bạn và sau đó liên kết với thuộc tính phụ thuộc này
Đây là XAML-Mã (Nhận DataContext của ListBox):
<UserControl
x:Name="MyControl">
<ListBox
DataContext="{Binding ElementName=MyControl}"
ItemsSource="{Binding ItemsSource}">
</ListBox>
</UserControl>
Đây là codeBehind:
public partial class MyCustomControl
{
public IEnumerable ItemsSource
{
get { return (IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable),
typeof(ToolboxElementView), new PropertyMetadata(null));
}
Đây là Mã, nơi bạn sử dụng "MyCustomControl" của bạn:
<Window>
<local:MyCustomControl
ItemsSource="{Binding MyItemsIWantToBind}">
</local:MyCustomControl>
</Window>
'newValueINotifyCollectionChanged' luôn là' null'. –
Khi tôi xóa danh sách bị chặn, OnItemsSourcePropertyChanged không được kích hoạt ... Có nên không? – user3260977