共享嵌套对象更新时更新所有父项

时间:2017-05-18 09:59:36

标签: wpf data-binding

我有以下类层次结构。

public class RowViewModel : BaseViewModel
    {
        private CellViewModel cell;
        public CellViewModel Cell
        {
            get { return cell; }
            set
            {
                cell = value;
                OnPropertyChanged("Cell");
            }
        }
    }

public class CellViewModel : BaseViewModel
    {
        public string Text { get { return string.Join("\n", CellLinks.Select(c => c.Text)); } }
        private ObservableCollection<CellLinkViewModel> cellLinks;
        public ObservableCollection<CellLinkViewModel> CellLinks
        {
            get
            { return cellLinks; }
            set
            {
                cellLinks = value;
                OnPropertyChanged("CellLinks");
            }
        }
    }
public class CellLinkViewModel : BaseViewModel
    {
        public string Text
        {
            get { return CellValue.Text; }
            set
            {
                CellValue.Text = value;
                OnPropertyChanged("Text");              
            }
        }
        public CellValueViewModel CellValue { get; set; }
    }
public class CellValueViewModel : BaseViewModel
    {
        public string Text { get; set; }
    }
public class BaseViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
                PropertyChanged(this, e);
            }
        }
    }

XAML代码看起来像,

<DataGrid ItemsSource="{Binding Rows}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="#" Width="200">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Cell.Text}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <ListBox ItemsSource="{Binding Cell.CellLinks}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Text, Mode=TwoWay}"/>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

CellValueViewModel在多个CellLinkViewModel之间共享。当我更改CellValueViewModel时,它应该将更改传播到所有依赖父项,DataGrid应显示所有最新值。

这是结果

This is result

但我认为它不会自动发生,我在这里遗漏了什么吗?如何更新嵌套对象时,如何通知所有datagrid单元格自动更新。

1 个答案:

答案 0 :(得分:1)

您需要将事件处理程序与所有PropertyChanged个对象的CellLinkViewModel事件挂钩,并针对PropertyChanged的{​​{1}}属性引发Text事件每当链接被修改时,例如:

CellViewModel

您可能还希望处理public class CellViewModel : BaseViewModel { public string Text { get { return string.Join("\n", CellLinks.Select(c => c.Text)); } } private ObservableCollection<CellLinkViewModel> cellLinks; public ObservableCollection<CellLinkViewModel> CellLinks { get { return cellLinks; } set { cellLinks = value; if(cellLinks != null) { foreach(var link in cellLinks) { link.PropertyChanged += Link_PropertyChanged; } } OnPropertyChanged("CellLinks"); } } private void Link_PropertyChanged(object sender, PropertyChangedEventArgs e) { OnPropertyChanged("Text"); } } 的{​​{1}}事件,并将事件处理程序连接到动态添加的CollectionChanged对象。