DataGrid不会更新ViewModel

时间:2012-12-25 22:42:11

标签: wpf mvvm wpfdatagrid

我有一个datagrid,它绑定到下面的ViewModel的ObservableCollection。 Datagrid将正确显示所有值,因此绑定似乎正在工作,但如果我更改某个值,Grid将不会调用我的VM的setter。有人可以告诉我为什么吗?

这是我的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; }
    }

}

以下是XAML代码:

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

谢谢大家!

2 个答案:

答案 0 :(得分:1)

UpdateSourceTrigger = Bindings上的PropertyChanged缺失。

答案 1 :(得分:0)

不确定为什么没有调用setter但是如果你使用依赖属性,他们实际上会被调用。我太醉了,无法调查为什么CLR属性没有在你的项目中设置,但这对我有用。

public partial class MainWindow : Window
{        public ObservableCollection<DocumentVm> Items
    {
        get { return (ObservableCollection<DocumentVm>)GetValue(ItemsProperty); }
        set { SetValue(ItemsProperty, value); }
    }
    public static readonly DependencyProperty ItemsProperty =
        DependencyProperty.Register("Items", typeof(ObservableCollection<DocumentVm>), typeof(MainWindow), new PropertyMetadata(null));

    public MainWindow()
    {
        InitializeComponent();
        Items = new ObservableCollection<DocumentVm>();
        Items.Add(new DocumentVm() { Name = "Name 1"});
        Items.Add(new DocumentVm() { Name = "Name 2"});
    }
}

public class DocumentVm : DependencyObject
{
    public string Name
    {
        get { return (string)GetValue(NameProperty); }
        set { SetValue(NameProperty, value); }
    }
    public static readonly DependencyProperty NameProperty =
        DependencyProperty.Register("Name", typeof(string), typeof(DocumentVm), new PropertyMetadata(null, new PropertyChangedCallback( (s, e)=>
            {
                // This will run when the property is set.
            })));        
}