Search code examples
wpfmvvmwpfdatagrid

DataGrid won't update ViewModel


I've got a datagrid which is bound to an ObservableCollection of the ViewModel below. The Datagrid will display all values correctly, so the binding seems to be working, but if I change some value the Grid won't call the setter's of my VM. Could somebody tell me why?

Here's my ViewModel:

    public class DocumentVm : ViewModelBase
{
    private Document document;
    public bool IsNew { get; private set; }
    public Document Document {
        get { return document; }
    }

    public DocumentVm(Document document)
    {
        this.document = document;
        IsNew = false;
    }

    public DocumentVm(Document document, bool isNew)
    {
        this.document = document;
        IsNew = isNew;
    }

    public String Name 
    {
        get { return document.Name; }
        set { document.Name = value; RaisePropertyChangedEvent("Name");}
    }

    public String Path
    {
        get { return document.Path; }
        set { document.Path = value; }
    }

    public String Metadata
    {
        get { return document.Metadata; }
        set { document.Metadata = value; }
    }

    public int SpeechId
    {
        get { return document.SpeechId; }
        set { document.SpeechId = value; }
    }

}

Here is the XAML Code:

<DataGrid Margin="3" Grid.Row="7" Grid.Column="1" BorderThickness="0" 
      ItemsSource="{Binding Path=CurrentSpeech.Documents, Mode=TwoWay}" 
      SelectedItem="{Binding Path=CurrentSpeech.CurrentDocument, Mode=TwoWay}">
<DataGrid.Columns>
    <DataGridTemplateColumn Header="Name" Width="SizeToCells">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Name, Mode=TwoWay}" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="MetaDaten" Width="SizeToCells">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Metadata, Mode=TwoWay}" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="Pfad" Width="SizeToCells">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path}" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

Thank you all!


Solution

  • UpdateSourceTrigger=PropertyChanged on the Bindings was missing.