我有一個Silverlight頁面,其中包含一個DataGrid。它綁定到一個ViewModel.On的ViewModel的初始化我已經調用RIA服務來從數據庫中獲取所有記錄。我有另一個按鈕頁面,它會在click上打開一個子窗體。該子窗體包含向數據庫添加記錄的DataForm,再次成功添加記錄後,我使用RIA Services和RaisedPropertyChanged事件獲取所有記錄。但DataGrid不顯示新記錄。什麼是問題,爲什麼DataGrid沒有得到刷新...視圖和viewmodel的代碼寫在下面。刷新DataGrid MVVM Silverlight
//XAML of View
<sdk:DataGrid x:Name="grd_classes" ItemsSource="{Binding Classes,Mode=TwoWay}" AutoGenerateColumns="False" Width="300" Grid.Column="1" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="Class Name" Width="140" Binding="{Binding Name,Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" />
<sdk:DataGridTextColumn Header="Alias" Width="140" Binding="{Binding Alias,Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
//Code of viewmodel
namespace SMS.ViewModel
{
public class ClassesViewModel:ViewModel
{
private ClassesContext _context = new ClassesContext();
public ClassesViewModel()
{
_context.Load<Class>(_context.GetClassesQuery(), OnLoad, true);
}
public EntitySet<Class> Classes
{
get
{
return _context.Classes;
}
}
public void AddNewClass(object parameter)
{
for (int i = 0; i <= newClass.Count - 1;i++)
{
_context.Classes.Add(newClass[i]);
}
_context.SubmitChanges(OnSave,null);
}
private void OnLoad(LoadOperation op)
{
if (!op.HasError)
{
RaisePropertyChanged("Classes");
}
}
private void OnSave(SubmitOperation op)
{
if (op.IsComplete)
{
if (op.HasError)
{
MessageBox.Show("Error");
}
else
{
_context = new ClassesContext();
_context.Load<Class>(_context.GetClassesQuery(), OnLoad, true);
DialogResult = true;
}
}
}
}
}
您是否在'OnLoad'中執行'RaisePropertyChanged'後檢查是否調用'Classes'的getter? – dmusial 2012-01-07 12:38:14
也驗證Classes.Count> 0 – codeSetter 2012-01-07 15:46:07