2012-02-27 17 views
16

Czy ktoś wie, jak zrobić niestandardowe ItemsSource?Własne ItemsSource właściwość UserControl

Co chcę zrobić, to zrobić itemsSource do mojego własnego UserControl, aby mógł być związany przez ObservableCollection<>.

Mogę też wiedzieć, ilekroć zaktualizowano liczbę elementów w itemsSource, aby wykonać dalsze procedury.

Dziękuję bardzo.

Odpowiedz

27

Być może trzeba zrobić coś takiego w swojej kontroli

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' jest zawsze' null'. –

+0

Po wyczyszczeniu listy ograniczonej wartość OnItemsSourcePropertyChanged nie zostanie wywołana ... Czy powinien nią być? – user3260977

3

Korzystanie DependencyProperty ItemsSource w CustomControl a następnie wiążą się z tym DependencyProperty

to XAML-Code (Rozpoznaj DataContext ListBox):

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

to kodzie:

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)); 
} 

Jest to kod, gdzie można korzystać z "MyCustomControl":

<Window> 
    <local:MyCustomControl 
     ItemsSource="{Binding MyItemsIWantToBind}"> 
    </local:MyCustomControl> 
</Window> 
Powiązane problemy