So i have fallen into a problem I am able to update values in a data grid when a textbox edits the value of the cell that the datagrid is selected on, this results in a UI update instantly. The pitfal is that i cannot update arbitrary cells that depend on this cell’s value. The data in which the cell/column is bound to changes yet the DataGrid Doesn’t update the depedent cells values. I have a lot of cells so it is very costly to call the DataGrid.Item.Refresh(). I have tried creating a method that just raises an InotifyProperty changed event yet this doesn’t cause an update in the grid. I am at a loss as how to force the GUI to update.
The code for the model cell is below. and the biding code is below it.
public class ModelCell : INotifyPropertyChanged {
//This is a class that represents a cell that a user clicks on. it contains few items. And should have a reference to the SpreadSheet object
public String CellName {
get;
set;
}
public IEnumerable<String> dependents {
get;
private set;
}
public String Contents {
get {
return Host.GetCellContents(CellName);
}
set {
if (value != _contents) {
IEnumerable<String> tmpDep = Host.SetCellContents(CellName, value);
try {
if (Host.GetCellValue(CellName) is SS.FormulaError) {
//Revert the change.
Host.SetCellContents(CellName, _contents);
} else {
_contents = value;
NotifyPropertyChanged("Contents");
NotifyPropertyChanged("Value");
//Next is to notify dependents that we have changed. Since the names will be in the Format of A1
this.dependents = tmpDep;
}
} catch (Exception e) {
//We got an exception no change was made to the sheet anyway.
MessageBox.Show(e.Message);
}
} else NotifyPropertyChanged("Value");
}
}
public int Row;
public int Col;
public override string ToString () {
return "THis is a test!";
}
/// <summary>
/// Default initialize the empty cell to empty contents. this makes the construction of the objects simpler;
/// </summary>
private String _contents = "";
/// <summary>
/// This value is contained in the SpreadSheet so it is automatically changed with the Contents.
/// </summary>
public String Value {
get {
try {
Object returned = Host.GetCellValue(CellName);
if (returned is String || returned is Double)
return returned.ToString();
return ((SS.FormulaError)returned).Reason;
} catch (Exception e) {
MessageBox.Show("Cell " + this.CellName + " encountered an exception getting the value: " + e.Message);
return "";
}
}
set {
//instead assign contents
NotifyPropertyChanged("Value");
}
}
public SS.Spreadsheet Host {
set;
private get;
}
/// <summary>
/// Creates an empty cell with no value or contents with the specified name.
/// </summary>
/// <param name="Name">The Name of this cell generally in the format [A-Z][1-99]</param>
public ModelCell (String Name) {
this.CellName = Name;
}
public ModelCell () {
//Name will equal the base.ToString()
this.CellName = base.ToString();
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged (String propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public MainWindow () {
InitializeComponent();
ViewModel = new MainViewModel();
DataGrid Sheet = null;
if (this.FindName("Sheet") is DataGrid)
Sheet = (DataGrid)this.FindName("Sheet");
if (Sheet == null)//Exit
this.Close();
IValueConverter converter = new ModelCellConverter();
Binding bind;
for (int i = 0; i < 26; i++) {
DataGridColumn col = new DataGridTextColumn();
col.Header = (char)('A' + i);
col.IsReadOnly = true;
col.Width = new DataGridLength(60);
col.CanUserSort = false;
//bind = new Binding(new String((char)('A' + i), 1));
bind = new Binding("indexAbleArr[" + i + "].Value");
//bind.Converter = new IdentityConverter();
bind.Mode = BindingMode.OneWay;
((DataGridTextColumn)col).Binding = bind;
Sheet.Columns.Add(col);
}
Sheet.ItemsSource = ViewModel.Rows;
//Current cell display
Label lab = null;
if (this.FindName("CurCell") is Label) {
lab = (Label)this.FindName("CurCell");
bind = new Binding("ActiveCell.CellName");
bind.Source = ViewModel;
bind.NotifyOnSourceUpdated = true;
lab.SetBinding(Label.ContentProperty, bind);
} //Don't bind the label if i cant find it
if (this.FindName("ValueBox") is Label) {
lab = (Label)this.FindName("ValueBox");
bind = new Binding("ActiveCell");
bind.Source = ViewModel;
bind.Converter = converter;
bind.NotifyOnSourceUpdated = true;
lab.SetBinding(Label.ContentProperty, bind);
}
TextBox content = null;
if (this.FindName("Content") is TextBox) {
content = (TextBox)this.FindName("Content");
bind = new Binding("ActiveCell.Contents");
bind.Source = ViewModel;
bind.Mode = BindingMode.TwoWay;
bind.NotifyOnSourceUpdated = true;
content.SetBinding(TextBox.TextProperty, bind);
}
ViewModel.ActiveCell = ViewModel.Rows[0].indexAbleArr[0];
}
I am using XAML To specify the Layout but no binding is done via XAML so nothing noteworthy is in my XAML code. Rows is a Object that contains an array of model cells (indexAbleArray) for the purposes of binding. It also precreates the 26 needed modelcells for the binding to not throw a million null pointer exceptions.
I think the problem is your portion of index based columns
indexAbleArr[" + i + "]in the binding Path of your DataGridTextColumns. They are not participating in the property change notifications.As BalamBalam suggested make that array into an
ObservableCollectionor you should be raising a property changed notification at row level for property “indexAbleArr” when any cell int that array changes itValue.Both to me are actually incomplete designs. 🙁