绑定到用户控件中的可观察集合并从中获取通知
本文关键字:合并 集合 通知 获取 观察 用户 控件 绑定 | 更新日期: 2023-09-27 18:37:27
我一直在查看其他一些看起来与我的问题相似的问题,但没有一个有答案。所以,在这里...
我正在使用 WPF 和 MVVM 模式开发一个 C# 应用程序。我有一个包含依赖属性的用户控件(称为组树):
public static DependencyProperty ChartGroupsProperty = DependencyProperty.Register("ChartGroups", typeof(ChartGroupCollection), typeof(GroupTree),
new FrameworkPropertyMetadata() { DefaultValue=new ChartGroupCollection(), PropertyChangedCallback = OnChartGroupsChanged, BindsTwoWayByDefault = true });
public ChartGroupCollection ChartGroups
{
get { return (ChartGroupCollection)GetValue(ChartGroupsProperty); }
set
{
SetValue(ChartGroupsProperty, value);
}
}
private static void OnChartGroupsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != e.NewValue)
{
// Nothing at the moment
}
}
它的类型是ChartGroupCollection,其定义如下:
public class ChartGroupCollection : ObservableCollection<ChartGroup>
{
}
ChartGroup 包含使用 INotifyProperty 的属性,我已经独立验证了应为主集合触发的所有更改事件是否正确。
使用该控件的 MainWindow.Xaml:
<Controls:GroupTree x:Name="Groups" ChartGroups="{Binding MainGroups}" />
我有另一个控件绑定到组树控件中的图表组值:
<Controls:ChartsControl x:Name="Charts1_1" Tree="{Binding ElementName=Groups, Path=ChartGroups}" />
我想要发生的是,当组树图表组值更改时,这会动态通知图表控件这些更改并相应地更新。但此刻,没有喜悦。数据在那里,因为如果手动强制刷新图表控件,则会显示更改。我尝试绑定到 MainGroups 属性,希望这会起作用,但这也没有。
我可能错过了一些显而易见的东西,但我不确定是什么。有什么想法吗?
抢

ObservableCollection 仅侦听集合中发生的更改,例如添加或删除的项目,并且不会通知其集合的各个项目中发生的任何更改。
下面的类通过将自身注册到要添加的项的 INofityPropertyChanged 事件来增强 ObservableCollection 的功能性,并在集合的项的属性更改时引发 ObservableCollection 的 CollectionChanged 事件。
最后调用 ClearItems 会释放所有事件处理程序。
更多详细信息可以从下面的链接中找到。
如何侦听可观察集合项的属性更改
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Collections;
namespace VJCollections
{
/// <summary>
/// This class adds the ability to refresh the list when any property of
/// the objects changes in the list which implements the INotifyPropertyChanged.
/// </summary>
/// <typeparam name="T">
public class ItemsChangeObservableCollection<T> :
ObservableCollection<T> where T : INotifyPropertyChanged
{
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
RegisterPropertyChanged(e.NewItems);
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
UnRegisterPropertyChanged(e.OldItems);
}
else if (e.Action == NotifyCollectionChangedAction.Replace)
{
UnRegisterPropertyChanged(e.OldItems);
RegisterPropertyChanged(e.NewItems);
}
base.OnCollectionChanged(e);
}
protected override void ClearItems()
{
UnRegisterPropertyChanged(this);
base.ClearItems();
}
private void RegisterPropertyChanged(IList items)
{
foreach (INotifyPropertyChanged item in items)
{
if (item != null)
{
item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
private void UnRegisterPropertyChanged(IList items)
{
foreach (INotifyPropertyChanged item in items)
{
if (item != null)
{
item.PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
}
}
}
private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
}
不幸的是,仅当针对绑定属性引发PropertyChanged时,才会调用PropertyChanged回调(从而导致绑定评估)。
您需要 INotifyCollectionChanged 提供的CollectionChanged事件。所以你有两个选择:
-
在
OnChartGroupsChanged中注册该事件并自行处理(确保取消注册旧事件,以免泄漏句柄! -
从类似
ItemsControl的东西派生并使用其Items属性。这将删除您的依赖项属性,并允许ItemsControl为您处理所有集合更改的内容。
如果可行,我会选择第二个。如果没有,利用CollectionChanged事件应该不会太糟糕。