0

In my program I have a DataGrid implemented through MVVM. Next to this DataGrid is a button that executes a command that I've named, "Fill Down". It takes one of the columns and copies a string to every cell in that column. The problem is that the view doesn't make the change until I change the page and then go back to the page with the DataGrid. Why is this happening, and what can I do to fix it?

xaml:

<Button Command="{Binding FillDown}" ... />
<DataGrid ItemsSource="{Binding DataModel.Collection}" ... />

ViewModel:

private Command _fillDown;

public ViewModel()
{
     _fillDown = new Command(fillDown_Operations);
}

//Command Fill Down
public Command FillDown { get { return _fillDown; } }
private void fillDown_Operations()
{
    for (int i = 0; i < DataModel.NumOfCells; i++)
    {
        DataModel.Collection.ElementAt(i).cell = "string";
    }
    //**I figured that Notifying Property Change would solve my problem...
    NotifyPropertyChange(() => DataModel.Collection);
}

-Please let me know if there is anymore code you would like to see.

Yes, sorry my Collection is an ObservableCollection

4

1 に答える 1

3

プロパティのセッターで NotifyPropertyChanged() を呼び出します。

public class DataItem
{
   private string _cell;
   public string cell //Why is your property named like this, anyway?
   {
       get { return _cell; }
       set
       {
           _cell = value;
           NotifyPropertyChange("cell");

           //OR

           NotifyPropertyChange(() => cell); //if you're using strongly typed NotifyPropertyChanged.
       }
   }
}

サイドコメント:

これを変える:

for (int i = 0; i < DataModel.NumOfCells; i++)
{
    DataModel.Collection.ElementAt(i).cell = "string";
}

これに:

foreach (var item in DataModel.Collection)
    item.cell = "string";

これははるかにクリーンで読みやすいです。

于 2013-10-28T17:59:03.060 に答える