2012-12-25 99 views
1

我有一個datagrid綁定到下面的ViewModel的ObservableCollection。 Datagrid將正確顯示所有值,所以綁定似乎正在工作,但如果我改變某個值,Grid將不會調用我的虛擬機的setter。有人能告訴我爲什麼嗎?DataGrid不會更新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> 

謝謝大家!

+0

我不能看到您的網格中的任何編輯模板。你如何通過UI進行編輯? –

回答

1

UpdateSourceTrigger = PropertyChanged on Bindings was missing。

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. 
      })));   
} 
+0

謝謝你,安迪!希望你沒有糟糕的宿醉。 :)找出問題了,我忘了在Bindings上設置UpdateSourceTrigger = PropertyChanged。 – Paxx

相關問題