2013-04-21 131 views
0

我是mvvm的新手,basicaly是我的第一次嘗試。現在我有一個WPF窗口,ado.net連接到我的數據庫,以及wcf服務來建立viewmodel和數據庫之間的連接。問題是我不能將數據從我的視圖添加到數據庫。 這裏的一些代碼 我的WCF方法:與MVVM綁定的問題

[OperationContract] 
    public void ManageOrder(Order order, EntityState state) 
    { 
     using (var context = new SvLaserEntities()) 
     { 
      context.Attach(order); 
      context.ObjectStateManager.ChangeObjectState(order, state); 
      context.SaveChanges(); 
     } 

    } 
    [OperationContract] 
    public void ManageClient(Client client, EntityState state) 
    { 
     using (var context = new SvLaserEntities()) 
     { 
      context.Attach(client); 
      context.ObjectStateManager.ChangeObjectState(client, state); 
      context.SaveChanges(); 
     } 

    } 

一種結合按鈕命令:爲文本框的一個

public ICommand AddClient 
    { 
     get 
     { 
      if ((addClient == null) && (CurrentClient != null)) 
      { 
       addClient = new RelayCommand(() => this.client.ManageClientAsync(CurrentClient, EntityState.Added)); 
      } 
      return addClient; 
     } 
    } 

和XAML代碼:

<TextBox HorizontalAlignment="Left" Height="28" 
         Margin="469,50,0,0" TextWrapping="Wrap" 
         VerticalAlignment="Top" Width="160" 
         Text="{Binding CurrentClient.Name, Mode=Default, UpdateSourceTrigger=PropertyChanged}" 
         /> 

直接的問題是,當我填充文本框並單擊添加它們,我在這裏捕獲空引用異常,在客戶端參數:

public void ManageClient(Client client, EntityState state) 

我在做什麼錯?

+0

實行的是它的客戶端或實體狀態一片空白?你可以發佈你的客戶類 – 2013-04-22 01:20:44

回答

1
在XAML

改變

模式屬性,結合雙向像

<TextBox Text="{Binding CurrentClient.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 

請確保INotifyPropertyChanged的是在視圖模型和模型

+0

嘗試過,仍然會得到相同的異常:/ CurrentClient屬性沒有填充輸入到文本框中的數據:(當它發送到數據庫時它只是空的。 – 2013-04-21 14:04:39

+0

是否在視圖模型和模型中實現了INotifyPropertyChanged – 2013-04-21 14:18:44

+0

是我做了,但在viewmodel中的onlu。模型我使用ado.net構建模型。 – 2013-04-21 14:24:25