UserControl的自定义ItemsSource属性

问题描述 投票:20回答:3

有谁知道如何制作自定义ItemsSource

我想做的是为我自己的itemsSource做一个UserControl,以便它可以被ObservableCollection<>绑定。

此外,我可以知道每当更新itemsSource中的项目数量,以便做进一步的程序。

非常感谢。

wpf data-binding itemssource
3个回答
34
投票

您可能需要在控件中执行此类操作

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.
}

7
投票

在CustomControl中使用DependencyProperty ItemsSource,然后绑定到此DependencyProperty

这是XAML-Code(识别ListBox的DataContext):

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

这是守则背后:

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

这是您使用“MyCustomControl”的代码:

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

0
投票

简化的答案。

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

    public static readonly DependencyProperty ItemsSourceProperty =
        DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(UserControl1), new PropertyMetadata(null, (s, e) =>
        {
            if (s is UserControl1 uc)
            {
                if (e.OldValue is INotifyCollectionChanged oldValueINotifyCollectionChanged)
                {
                    oldValueINotifyCollectionChanged.CollectionChanged -= uc.ItemsSource_CollectionChanged;
                }

                if (e.NewValue is INotifyCollectionChanged newValueINotifyCollectionChanged)
                {
                    newValueINotifyCollectionChanged.CollectionChanged += uc.ItemsSource_CollectionChanged;
                }
            }
        }));

    private void ItemsSource_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        // Logic Here
    }

    // Do Not Forget To Remove Event On UserControl Unloaded
    private void UserControl1_Unloaded(object sender, RoutedEventArgs e)
    {
        if (ItemsSource is INotifyCollectionChanged incc)
        {
            incc.CollectionChanged -= ItemsSource_CollectionChanged;
        }
    }
推荐问答
热门问答
最新问答