Почему не обновляются данные в первой таблице (MyItems), когда я редактирую (добавляю, удаляю, изменяю) вторую таблицу (MySubItems)?
MainWindow.xaml
<Window x:Class="CollectionBindingsExample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:l="clr-namespace:CollectionBindingsExample"
Title="SubCollectionSample" Height="350" Width="525" FontSize="22" WindowStartupLocation="CenterScreen">
<Window.Resources>
<l:MainWindowViewModel x:Key="mainWindowViewModel" />
<l:CollectionToString x:Key="collectionToString" />
</Window.Resources>
<Window.DataContext>
<Binding Source="{StaticResource mainWindowViewModel}" />
</Window.DataContext>
<Grid DataContext="{Binding Path=Collection}">
<Grid.RowDefinitions>
<RowDefinition Height="1*"></RowDefinition>
<RowDefinition Height="1*"></RowDefinition>
</Grid.RowDefinitions>
<GroupBox Grid.Row="0" Header="MyItems">
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="1*" Binding="{Binding Name}"
/>
<DataGridTextColumn Header="Collection" Width="1*" Binding="{Binding SubCollection, Converter={StaticResource collectionToString}}"
/>
</DataGrid.Columns>
</DataGrid>
</GroupBox>
<GroupBox Grid.Row="1" Header="MySubItems">
<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=SubCollection}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="1*" Binding="{Binding Name}"
/>
</DataGrid.Columns>
</DataGrid>
</GroupBox>
</Grid>
</Window>
MainWindowViewModel.cs
using System;
using System.Text;
using System.Windows.Data;
using System.Globalization;
namespace CollectionBindingsExample
{
class MainWindowViewModel
{
public MainWindowViewModel()
{
Collection = new Collection();
Collection.Add(new MyItem());
Collection.Add(new MyItem());
Collection.Add(new MyItem());
int i = 0;
foreach (MyItem item1 in Collection)
{
item1.Name = "Item" + i;
item1.SubCollection.Add(new MySubItem());
item1.SubCollection.Add(new MySubItem());
int j = 0;
foreach (MySubItem item2 in item1.SubCollection)
{
item2.Name = "SubItem" + i + "_" + j;
j++;
} i++;
}
}
public Collection Collection
{
get;
private set;
}
}
[ValueConversion(typeof(SubCollection), typeof(String))]
public class CollectionToString : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
SubCollection collection = (SubCollection)value;
StringBuilder s = new StringBuilder();
foreach (MySubItem v in collection)
{
s = s.Append(v.Name).Append(", ");
} return s.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
}
Models.cs
using System.Collections.ObjectModel;
namespace CollectionBindingsExample
{
class MyItem
{
public MyItem()
{
SubCollection = new SubCollection();
}
public string Name
{
get;
set;
}
public SubCollection SubCollection
{
get;
private set;
}
}
class Collection : ObservableCollection<MyItem>
{
}
class MySubItem
{
public string Name
{
get;
set;
}
}
class SubCollection : ObservableCollection<MySubItem>
{
}
}
По совету Ilya Pirogov'а я в Models.cs реализовал NotifyPropertyChanged. Все равно не работает.
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
namespace CollectionBindingsExample
{
class MyItem : INotifyPropertyChanged
{
public MyItem()
{
SubCollection = new SubCollection();
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; NotifyPropertyChanged("Name"); }
}
public SubCollection SubCollection { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string type)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(type));
}
}
}
class Collection : ObservableCollection<MyItem> { }
class MySubItem: INotifyPropertyChanged
{
public MySubItem() { }
private string _name;
public string Name
{
get { return _name; }
set { _name = value; NotifyPropertyChanged("Name"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string type)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(type));
}
}
}
class SubCollection : ObservableCollection<MySubItem>
{
public SubCollection() { }
}
}