繁体   English   中英

如何在UserControl中绑定集合依赖项属性

[英]How to bind collection dependency property in UserControl

这不是重复的! 当我失败时,我尝试寻找类似的帖子,但没有成功。 我不明白为什么未调用OnUCItemsSourceChanged 我很确定我错过了一些简单的事情,但是找不到。

我有一个包含UserControl1 Window ,该Window具有附加的集合属性,该属性绑定到WindowWindowCollection集合。 我希望在向WindowCollection添加项目时调用UserControl1.OnUCItemsSourceChanged 但这不会发生。

我想念什么?

Window1.xaml.cs

public partial class Window1 : Window
{
    public ObservableCollection<long> WindowCollection { get; set; }
    public Window1()
    {
        InitializeComponent();

        DataContext = this;

        WindowCollection = new ObservableCollection<long>();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        WindowCollection.Add(1);
        WindowCollection.Add(2);
    }
}

Window1.xaml

<StackPanel>
    <uc:UserControl1 UCItemsSource="{Binding Path=WindowCollection}" />
    <Button Content="Refresh" Click="Button_Click" />
</StackPanel>

UserControl1.xaml.cs

public static readonly DependencyProperty UCItemsSourceProperty = DependencyProperty.Register("UCItemsSource", typeof(IEnumerable), typeof(UserControl1), new PropertyMetadata(null, new PropertyChangedCallback(OnUCItemsSourceChanged)));

public IEnumerable UCItemsSource
{
    get { return (IEnumerable)GetValue(UCItemsSourceProperty ); }
    set { SetValue(UCItemsSourceProperty , value); }
}

public ObservableCollection<MyItem> UCItems { get; set; }

private static void OnUCItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var control = d as UserControl1;
    var items = e.NewValue as ObservableCollection<long>;

    foreach (var item in items)
   {
         control.UCItems.Add(new MyItem(item));
   }
}

UserControl1.xaml

<ItemsControl ItemsSource="{Binding UCItems}" ... />

更新这是我的测试项目的链接

在这一行:

<ItemsControl ItemsSource="{Binding UCItems}" ... />

必须是带有FindAncestor的RelativeSource ,因为UCItems位于UserControl中:

UserControl

<ItemsControl ItemsSource="{Binding Path=UCItems,
                                    RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />

I cannot understand why OnUCItemsSourceChanged is not called?

如果添加RelativeSource构造,则OnUCItemsSourceChanged至少OnUCItemsSourceChanged引起一次,因为PropertyChangedCallback每次都会触发,因此您需要为依赖项属性设置新值:

表示当依赖项属性的有效属性值更改时调用的回调。

由于您曾经在此处设置依赖项属性的值:

<uc:UserControl1 UCItemsSource="{Binding Path=WindowCollection}" />

I expect UserControl1.OnUCItemsSourceChanged to be called when I add items to WindowCollection.

为此,存在一个ObservableCollection<T>.CollectionChanged事件,其中包含对集合执行的动作的枚举:

在添加,删除,更改,移动项目或刷新整个列表时发生。

对于您的情况将是这样的:

Version with CollectionChanged

MainWindow

public partial class MainWindow : Window
{
    public ObservableCollection<long> WindowCollection { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;

        WindowCollection = new ObservableCollection<long>();

        WindowCollection.Add(1);
        WindowCollection.Add(2);            
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        WindowCollection.Add(3);
        WindowCollection.Add(4);
    }
}

UserControl

public partial class UserControl1 : UserControl
{
    #region Public Section

    public ObservableCollection<long> UCItems { get; set; }
    public static UserControl1 control;

    #endregion

    public UserControl1()
    {
        InitializeComponent();
        UCItems = new ObservableCollection<long>();            
    }

    #region UCItemsSource Property

    public static readonly DependencyProperty UCItemsSourceProperty = DependencyProperty.Register("UCItemsSource", 
                                                                                                  typeof(IEnumerable), 
                                                                                                  typeof(UserControl1),
                                                                                                  new PropertyMetadata(null, new PropertyChangedCallback(OnUCItemsSourceChanged)));

    public IEnumerable UCItemsSource
    {
        get { return (IEnumerable)GetValue(UCItemsSourceProperty); }
        set { SetValue(UCItemsSourceProperty, value); }
    }

    #endregion

    private static void OnUCItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        control = d as UserControl1;
        var items = e.NewValue as ObservableCollection<long>;

        items.CollectionChanged += new NotifyCollectionChangedEventHandler(CollectionChanged);
        AddItem(control, items);
    }

    private static void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        var items = sender as ObservableCollection<long>;
        control.UCItems.Clear();

        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            AddItem(control, items);
        }
    }

    private static void AddItem(UserControl1 userControl, ObservableCollection<long> collection) 
    {
        if (collection.Count > 0)
        {
            foreach (var item in collection)
            {
                userControl.UCItems.Add(item);
            }
        }
    }
}

该项目在此link可用

Alternative version

此版本更简单,更正确。 在这里,我们只引用包含集合的UCItemsSource属性,在这里还用RelativeSource证明了这一点:

UserControl

XAML

<Grid>
    <ItemsControl ItemsSource="{Binding Path=UCItemsSource, 
                                        RelativeSource={RelativeSource Mode=FindAncestor,
                                                                       AncestorType={x:Type UserControl}}}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" />
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

Code-behind

public partial class UserControl1 : UserControl
{
    #region Public Section

    public ObservableCollection<long> UCItems { get; set; }

    #endregion

    public UserControl1()
    {
        InitializeComponent();

        UCItems = new ObservableCollection<long>();
    }

    #region UCItemsSource Property

    public static readonly DependencyProperty UCItemsSourceProperty = DependencyProperty.Register("UCItemsSource", 
                                                                                                  typeof(IEnumerable), 
                                                                                                  typeof(UserControl1));                                                                                                      

    public IEnumerable UCItemsSource
    {
        get { return (IEnumerable)GetValue(UCItemsSourceProperty); }
        set { SetValue(UCItemsSourceProperty, value); }
    }

    #endregion
}

尝试这个

private ObservableCollection<long> _windowCollection 
public ObservableCollection<long> WindowCollection 
{ 
   get { return _windowCollection; }
   set
   {
      _windowCollection = value;
      RaiseOnPropertyChange(() => WindowCollection);
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM