I want to display a list of notes from an object in a WPF DataGrid using MVVM
XAML:
<DataGrid
x:Name="NoteGrid"
ItemsSource="{Binding NoteObj.Notes}"
SelectedItem="{Binding CurrentNote}"
AutoGenerateColumns="False"
CanUserAddRows="False"
CellEditEnding="DataGrid_CellEditEnding">
<DataGrid.Columns>
<DataGridTextColumn Header="Note" Binding="{Binding NoteText}" />
<DataGridTextColumn Header="Type" Binding="{Binding Type.Name}" />
</DataGrid.Columns>
</DataGrid>
The return-value of NoteObj.Notes is EntitySet.
ViewModel:
private NoteObject noteObj;
public NoteObject NoteObj
{
get { return noteObj; }
set { noteObj = value; OnPropertyChanged("NoteObj"); }
}
public void AddNote()
{
var note = new Note
{
NoteText = "Note text",
NoteType = 1
};
NoteObj.Notes.Add(note);
DC.SubmitChanges();
OnPropertyChanged("NoteObj");
}
When NoteObj is set the DataGrid is filled with notes but the AddNote-method doesn’t wotk!
The new note is added to the database, but the DataGrid is never updated.
Is it a problem eith EntitySet or am I missing something in the XAML?
This will only work if
NoteObj.NotesimplementsINotifyCollectionChanged. You can achieve this by using the classObservableCollection<T>.