2012-02-27 27 views
16

Có ai biết cách thực hiện một tùy chỉnh ItemsSource không?Mục tùy chỉnhSở sản phẩm cho một UserControl

Điều tôi muốn làm là tạo itemsSource thành UserControl của riêng mình để có thể bị ràng buộc bởi ObservableCollection<>.

Ngoài ra, tôi có thể biết Mỗi khi số lượng mục trong số itemsSource được cập nhật, để thực hiện thêm các thủ tục.

Cảm ơn bạn rất nhiều.

Trả lời

27

Bạn có thể cần phải làm một cái gì đó như thế này trong kiểm soát của bạn

public IEnumerable ItemsSource 
{ 
    get { return (IEnumerable)GetValue(ItemsSourceProperty); } 
    set { SetValue(ItemsSourceProperty, value); } 
} 

public static readonly DependencyProperty ItemsSourceProperty = 
    DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(UserControl1), new PropertyMetadata(new PropertyChangedCallback(OnItemsSourcePropertyChanged))); 

private static void OnItemsSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
{ 
    var control = sender as UserControl1; 
    if (control != null) 
     control.OnItemsSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue); 
} 



private void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue) 
{ 
    // Remove handler for oldValue.CollectionChanged 
    var oldValueINotifyCollectionChanged = oldValue as INotifyCollectionChanged; 

    if (null != oldValueINotifyCollectionChanged) 
    { 
     oldValueINotifyCollectionChanged.CollectionChanged -= new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged); 
    } 
    // Add handler for newValue.CollectionChanged (if possible) 
    var newValueINotifyCollectionChanged = newValue as INotifyCollectionChanged; 
    if (null != newValueINotifyCollectionChanged) 
    { 
     newValueINotifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged); 
    } 

} 

void newValueINotifyCollectionChanged_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
{ 
    //Do your stuff here. 
} 
+0

'newValueINotifyCollectionChanged' luôn là' null'. –

+0

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

3

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> 
Các vấn đề liên quan