DataGrid仅更新选定的单元格

时间:2011-10-03 18:58:52

标签: c# wpf xaml binding wpfdatagrid

所以我遇到了一个问题,当文本框编辑选中数据网格的单元格的值时,我能够更新数据网格中的值,这会立即导致UI更新。 pitfal是我无法更新依赖于此单元格值的任意单元格。单元格/列绑定的数据发生更改但DataGrid不更新依赖单元格值。我有很多单元格,因此调用DataGrid.Item.Refresh()非常昂贵。我尝试创建一个只引发InotifyProperty更改事件的方法,但这不会导致网格中的更新。我不知道如何强制GUI更新。

模型单元的代码如下。而且招标代码在它下面。

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

我正在使用XAML指定布局但是没有通过XAML完成绑定,因此我的XAML代码中没有任何值得注意的内容。 Rows是一个Object,它包含一组模型单元格(indexAbleArray),用于绑定。它还为绑定预先创建了26个所需的模型单元,而不是抛出一百万个空指针异常。

1 个答案:

答案 0 :(得分:0)

我认为问题是DataGridTextColumns的绑定路径中基于索引的列indexAbleArr[" + i + "]的一部分。他们没有参与房产变更通知。

正如BalamBalam建议将该数组放入ObservableCollection或者您应该在行级别为属性“indexAbleArr”引发属性更改通知,当该数组的任何单元格更改它Value时。

对我而言实际上都是不完整的设计。 :(