WPF通知链实现

WPF通知链实现,wpf,mvvm,binding,notify,Wpf,Mvvm,Binding,Notify,模型如下: public class Customer { string Name; public ObservableCollection<Order> orders; } public class Order { int Id; public ObservableCollection<RequestStorage> Requests; } public class RequestStorage { string Text

模型如下:

public class Customer
{
    string Name;
    public ObservableCollection<Order> orders;
}

public class Order 
{ 
    int Id;
    public ObservableCollection<RequestStorage> Requests;
}

public class RequestStorage
{
    string Text;
    public ObservableCollection<Response> Responses;
}

public class Response
{
    string Text;
}

Q:当响应集合已更改时,例如添加了新响应,以便CustomerView.xaml将更新指定客户的UI时,我如何通知CustomerViewModel?

我现在实现的解决方案是一个类CustomerRegister,它包含列表,并提供获取/添加/编辑/保存a.s.o客户对象的方法。CustomerViewModel cantains CustomerRegister。 此外,当事件处理程序进行任何更改时,CustomerRegister会通知CustomerViewModel。 最后,模型中的每个对象在其属性发生更改时都会通知CustomerRegister。
这似乎很复杂,所以我希望有其他选择。

好的,这是我用来听子集合变化的类。也许你可以用它

公共抽象类ChildListenerBase { 公共无效注册表ChildCollectionObservableCollection集合 其中T:INotifyPropertyChanged { //注册到集合已更改事件 collection.CollectionChanged+=OnCollectionChangedInternal; //注册到项目 集合中的每个T项 item.PropertyChanged+=OnItemPropertyChanged; } public void UnregisterChildCollectionObservableCollection集合 其中T:INotifyPropertyChanged { //取消注册到集合已更改事件 collection.CollectionChanged-=OnCollectionChangedInternal; //注销项目 集合中的每个T项 item.PropertyChanged-=OnItemPropertyChanged; } CollectionChangedInternalObject发送方的私有无效,通知CollectionChangedEventArgs e { //也许你还得再加一些案子 开关e.动作 { 案例NotifyCollectionChangedAction。添加: e.NewItems中的foreach对象项 INotifyPropertyChanged item.PropertyChanged+=OnItemPropertyChanged; 打破 案例NotifyCollectionChangedAction。删除: e.OldItems中的foreach对象项 INotifyPropertyChanged item.PropertyChanged-=OnItemPropertyChanged; 打破 } OnCollectionChangedsender,e; } CollectionChangedObject发送方、NotifyCollectionChangedEventArgs e上受保护的虚拟无效 { //重写派生类中的方法以处理其他内容 } //在此处执行对项目更改的反应 受保护的抽象数据无效。不动产变更对象发送方,不动产变更对象发送方; }
你能发布CustomerViewModel吗?多一点信息就更容易帮助你。你到底想达到什么目的?如果视图的设计是好的,您不需要通知CustomerView,因为您将拥有一些可视化响应集合的东西,例如ResponseList。在这种情况下,您将拥有自动刷新的item DataTemplate。1。我有一个DataContext=CustomersViewModel的CustomersView.xaml。2.CustomersView.xaml有一个带有ItemsSource=Customers的数据网格。3.DataGrid列客户名称的绑定方式与Binding={Binding Path=name}4类似。DataGrid.RowDetails具有绑定到请求和响应的DataGrid。5.当应用程序启动时,一切正常,所有数据都按其必须的方式显示。6.当我编程添加新响应时,DataGrid中的数据不会更新。当数据更改时,我需要即时更新UI。好的,请向我们展示CustomersView.xaml,如果可能的话,只显示绑定的重要部分。我相信,不需要实现这个通知链就可以解决这个问题。相信我,你真的不想实施它;响应列表也应该是一个可观察的集合。ObservableCollection实现INotifyPropertyChanged,因此当您添加/删除项目时,UI将得到更新。请发布代码好吗?我只是感兴趣,自从我自己开发了一种方法以来,你是如何解决的:o@DHN,Im使用CustomerRegister中的standart EventHandler和Update方法更新ViewModel。我不确定你会用它。它工作正常,但更新CustomerView用户界面几乎需要10秒钟。我会继续寻找…我把我的班级贴给你了。我不确定这对你是否有帮助。
<!-- language: c# -->
public class CustomersViewModel
{
    public CustomersViewModel()
    {
        //Load customers from database to Customers
    }

    public ObservableCollection<Customer> Customers { get; set; }
}
<DataGrid ItemsSource="{Binding Path=Customers}">
    ...
    <DataGridTextColumn Header="Customer name" Binding="{Binding Path=Name}" />
    ...
    <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <DataGrid ItemsSource="{Binding Path=Orders}">
                        ...
                        <DataGridTextColumn Binding="{Path=Id}" />
                        <DataGridTemplateColumn>
                            <ItemsControl Binding="{Path=Requests}" />
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn>
                            <ItemsControl DataContext="{Binding Requests}" Binding="{Path=Responses}" />
                        </DataGridTemplateColumn>
                    </DataGrid>
                </DataTemplate>
    </DataGrid.RowDetailsTemplate>
</DataGrid>