当项目添加到ObservableCollection时,Datagrid不会更新

时间:2013-01-21 11:28:04

标签: wpf data-binding datagrid observablecollection

我有一个拥有Notes集合的Client实体。

我的View中有一个DataGrid,它与我的Client.AuditableNotes集合数据绑定,如果它们已存在于数据库中,我可以看到它们。但是,如果我将新的AuditableNote添加到集合中,它不会出现?

我的ClientEditViewModel具有以下客户端属性:

    //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 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>

0 个答案:

没有答案