【发布时间】:2011-08-24 09:23:47
【问题描述】:
我想使用 MVVM 在 WPF DataGrid 中显示来自对象的注释列表
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>
NoteObj.Notes的返回值为EntitySet。
视图模型:
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");
}
当 NoteObj 被设置时,DataGrid 会填充笔记,但 AddNote 方法不起作用! 新注释已添加到数据库中,但 DataGrid 从未更新。
是 EntitySet 的问题还是我在 XAML 中遗漏了什么?
【问题讨论】:
-
尝试添加presentationtracesources,看看它是否在输出中给你一些提示。
-
如果我将 TraceLevel 设置为“高”或“中”,程序会在 SubmitChanges 上崩溃(未找到行或其他原因)。没有输出!
-
这可能说明EF有问题,不绑定?
-
我使用了一个由 VS2010 自动生成的 LINQ to SQL 数据模型。
标签: wpf xaml data-binding mvvm datagrid