我有一个拥有一系列笔记的客户实体。
我的视图中有一个 DataGrid,它数据绑定到我的 Client.AuditableNotes 集合,如果它们已经存在于数据库中,我可以看到这些注释。但是,如果我将新的 AuditableNote 添加到集合中,它不会出现吗?
我的 ClientEditViewModel 对我的 Client 属性具有以下内容:
//Backing Field
private Client _client;
.
.
.
//Property
public Client Client
{
get { return _client; }
set
{
_client = value;
RaisePropertyChanged("Client");
}
}
//Upon my NoteEdit usercontrol returning control back to clienteditviewmodel
public void NoteEditFinished(AuditableNote note)
{
if (note != null)
{
note.AddedBy = _dbContext.SystemUsers.First();
if (note.AuditableNoteId == 0)
{
if (this.Client.AuditableNotes != null)
{
this.Client.AuditableNotes.Add(note);
}
else
{
List<AuditableNote> notes = new List<AuditableNote>();
notes.Add(note);
this.Client.AuditableNotes = notes;
}
}
else
{
AuditableNote existingNote = this.Client.AuditableNotes.Where(an => an.AuditableNoteId == note.AuditableNoteId).First();
existingNote = note;
}
}
}
我的 Datagrid 被定义为:
<DataGrid Name="grdNotes" MinWidth="180"
ItemsSource="{Binding Client.AuditableNotes, Mode=TwoWay}"
AutoGenerateColumns="False"
Background="Transparent"
RowBackground="Transparent"
IsReadOnly="True"
SelectedItem="{Binding SelectedNote}"
HorizontalScrollBarVisibility="Auto"
DataContext="{Binding}">
<DataGrid.Columns>
<DataGridTemplateColumn
Header="Warning">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image x:Name="MyImage" Width="25" Height="20"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Warning}" Value="True">
<Setter Property="Source" TargetName="MyImage" Value="/Assets/Images/symbol_error.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding Warning}" Value="False">
<Setter Property="Source" TargetName="MyImage" Value="/Assets/Images/symbol_information.png"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Note" Binding="{Binding Note, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="290"/>
<DataGridTextColumn Header="Added By" Binding="{Binding AddedBy, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="Date Added" Binding="{Binding DateAdded, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/>
</DataGrid.Columns>
</DataGrid>