We initially tried that but ran into a problem, where if the data source is modified then the grid cell is not updated until you explicitly call refresh. Here is the updated code which shows the problem.
ObservableCollection<MyData> list = new ObservableCollection<MyData>();
list.Add(new MyData("Test 1"));
list.Add(new MyData("Test 2"));
list.Add(new MyData("Test 3"));
DataGridCollectionView dataGridCollectionView = new DataGridCollectionView(list, typeof(MyData), false, false);
DataGridItemProperty datagridItem = new DataGridItemProperty();
datagridItem.Name = "Str";
datagridItem.Title = "My Data 1";
datagridItem.DataType = typeof(string);
dataGridCollectionView.ItemProperties.Add(datagridItem);
datagridItem = new DataGridItemProperty();
datagridItem.Name = "Str2";
datagridItem.ValuePath = "Str";
datagridItem.Title = "My Data 2";
datagridItem.DataType = typeof(string);
dataGridCollectionView.ItemProperties.Add(datagridItem);
grid.ItemsSource = dataGridCollectionView;
Our data class, MyData, implements INotifyPropertyChanged and looks like this:
public class MyData : INotifyPropertyChanged
{
string s;
public string Str
{
get { return s; }
set
{
if (s != value)
{
s = value;
NotifyPropertyChanged("Str");
}
}
}
public MyData(string _s)
{
s = _s;
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
Now both columns should bind to the single property named "Str". Let's say the data source changes (i.e. the value of Str property changes due to some other user action), then the first column cell is updated immediately, but the second column cell is not updated until you call Refresh on the grid items. Seems like using ValuePath of DataGridItemProperty causes some problems with binding. How can we resolve that?
Thanks.