这不是重复的!当我失败时,我试图寻找类似的帖子,但没有成功。我不明白为什么没有调用OnUCItemsSourceChanged
?我很确定我会错过一些简单但我找不到的东西。
我的Window
包含UserControl1
,其附加了绑定到Window
WindowCollection
集合的集合属性。我希望在向UserControl1.OnUCItemsSourceChanged
添加项目时调用WindowCollection
。但它并没有发生。
我想念的是什么?
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}" ... />
更新 这是我的测试项目link
答案 0 :(得分:1)
在这一行:
<ItemsControl ItemsSource="{Binding UCItems}" ... />
FindAncestor必须为RelativeSource
,因为位于UserControl中的 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
}
答案 1 :(得分:0)
试试这个
private ObservableCollection<long> _windowCollection
public ObservableCollection<long> WindowCollection
{
get { return _windowCollection; }
set
{
_windowCollection = value;
RaiseOnPropertyChange(() => WindowCollection);
}
}