2012-02-27 10 views
16

¿Alguien sabe cómo hacer un ItemsSource personalizado?Propiedad de Custom ItemsSource para un UserControl

Lo que quiero hacer es hacer un itemsSource en mi propio UserControl para que pueda estar limitado por ObservableCollection<>.

Además, podría saber cada vez que se actualice el número de elementos en el itemsSource, para poder realizar otros procedimientos.

Muchas gracias.

Respuesta

27

Es posible que tenga que hacer algo como esto en su control

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' es siempre null'. –

+0

Cuando borro la lista limitada, OnItemsSourcePropertyChanged no se activa ... ¿Debería ser? – user3260977

3

Utilice un DependencyProperty ItemsSource en su CustomControl y luego unirse a este DependencyProperty

Este es el XAML-Code (Reconocer el DataContext de el ListBox):

<UserControl 
    x:Name="MyControl"> 
    <ListBox 
     DataContext="{Binding ElementName=MyControl}" 
     ItemsSource="{Binding ItemsSource}"> 
    </ListBox> 
</UserControl> 

Este es el 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)); 
} 

este es el código, en el que utiliza su "MyCustomControl":

<Window> 
    <local:MyCustomControl 
     ItemsSource="{Binding MyItemsIWantToBind}"> 
    </local:MyCustomControl> 
</Window> 
Cuestiones relacionadas