1

这是模型:

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;
}

绑定到CustomersViewModel的CustomersView.xaml具有一个属性ObservableCollection<*Customer*> Customers;

客户视图模型

<!-- language: c# -->
public class CustomersViewModel
{
    public CustomersViewModel()
    {
        //Load customers from database to Customers
    }

    public ObservableCollection<Customer> Customers { get; set; }
}

CustomersView.xaml 窗口DataContext设置为CustomersViewModel

<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>

问:当 Responses 集合发生变化(例如添加了新的 Response)时,如何通知CustomersViewModel以便CustomersView.xaml为指定的客户更新其 UI?

4

2 回答 2

0

我现在实现的解决方案是一个CustomerRegister包含List<Customer>并提供获取/添加/编辑/保存 asoCustomer对象的方法的类。CustomersViewModel小酒馆CustomersRegister。此外,在上升事件处理程序进行任何更改时CustomerRegister通知。CustomersViewModel最后,“模型”中的每个都在其属性更改时object发出通知。CustomerRegister这似乎很复杂,所以我将不胜感激任何其他选择。

于 2013-05-23T21:52:55.180 回答
0

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

public abstract class ChildListenerBase
{
    public void RegisterChildCollection<T>(ObservableCollection<T> collection)
        where T : INotifyPropertyChanged
    {
        // register to collection changed event
        collection.CollectionChanged += OnCollectionChangedInternal;

        // register to items
        foreach (T item in collection)
            item.PropertyChanged += OnItemPropertyChanged;
    }

    public void UnregisterChildCollection<T>(ObservableCollection<T> collection)
        where T : INotifyPropertyChanged
    {
        // unregister to collection changed event
        collection.CollectionChanged -= OnCollectionChangedInternal;

        // unregister to item
        foreach (T item in collection)
            item.PropertyChanged -= OnItemPropertyChanged;
    }

    private void OnCollectionChangedInternal(object sender, NotifyCollectionChangedEventArgs e)
    {
        // probably you have to add some more cases
        switch (e.Action)
        {
            case NotifyCollectionChangedAction.Add:
                foreach (object item in e.NewItems)
                    ((INotifyPropertyChanged) item).PropertyChanged += OnItemPropertyChanged;
                break;
            case NotifyCollectionChangedAction.Remove:
                foreach (object item in e.OldItems)
                    ((INotifyPropertyChanged) item).PropertyChanged -= OnItemPropertyChanged;
                break;
        }

        OnCollectionChanged(sender, e);
    }

    protected virtual void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        // override method in derived class to handle some additional stuff
    }

    // implement the reaction on item changes here
    protected abstract void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e);
}
于 2013-05-25T06:08:19.093 回答