这不是重复的!当我失败时,我试图寻找类似的帖子,但没有成功。我不明白为什么不叫OnUCItemsSourceChanged
?我很确定我错过了一些简单的东西,但我找不到它。
我有Window
包含UserControl1
,其中包含绑定到Window
WindowCollection
集合的附加集合属性。我希望在将项目添加到WindowCollection
时调用UserControl1.OnUCItemsSourceChanged
。但它不会发生。
我想念什么?
窗口1.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);
}
}
窗口1.xaml
<StackPanel>
<uc:UserControl1 UCItemsSource="{Binding Path=WindowCollection}" />
<Button Content="Refresh" Click="Button_Click" />
</StackPanel>
用户控制1.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));
}
}
用户控制1.xaml
<ItemsControl ItemsSource="{Binding UCItems}" ... />
更新这是链接到我的测试项目
在这一行中:
<ItemsControl ItemsSource="{Binding UCItems}" ... />
必须与 FindAncestor RelativeSource
,因为UCItems
位于用户控件中:
UserControl
<ItemsControl ItemsSource="{Binding Path=UCItems,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />
I cannot understand why OnUCItemsSourceChanged is not called?
如果添加 RelativeSource 构造,则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);
}
}