2014-10-28 32 views
0

您知道何時將dataSources窗口中的表拖放到usercontrol? Visual Studio會自動創建一個datacontext(在視圖的xaml中)以綁定頁面上的新元素。此dataContext設置爲collectionViewSource,此collectionViewSource從數據集中繪製其信息。從拖放時拿走表格的相同數據集。使用linq將表中的行保存到另一個表中

在我的情況下這是它創建:

<UserControl.Resources> 
    <CollectionViewSource x:Key="signupsViewSource" Source="{Binding signups, Source={StaticResource myAppnDataSet}}/> 
</UserControl.Resources> 

<Grid DataContext="{StaticResource signupsViewSource}"> 
    <TextBox x:Name="idTextBox" Text="{Binding id, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/> 
    <TextBox x:Name="firstnameTextBox" Text="{Binding firstname, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/> 
    <TextBox x:Name="surnameTextBox" Text="{Binding surname, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/> 
    <TextBox x:Name="usernameTextBox" Text="{Binding username, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/> 
    <TextBox x:Name="passwordTextBox" Text="{Binding password, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true, UpdateSourceTrigger=PropertyChanged}"/> 
</Grid> 

這應該是一個非常簡單註冊頁面,管理員可以使用批准和拒絕通過簡單地滾動取得註冊,點擊確認按鈕或點擊拒絕按鈕。

事情是我正在使用MVVM模式。這意味着上面的textBoxes綁定到的屬性位於collectionViewSource類中,這是我的問題開始的地方。

我需要將這個相同的信息從'註冊'表保存到users表中。

  1. 由於textBoxes綁定的屬性在collectionViewSource中,有沒有什麼辦法從那裏獲取它們的值?
  2. 我通過解決方案資源管理器查看,沒有找到任何實際的類叫做signupViewSource。如果它不存在,那麼它是如何被引用的,爲什麼它甚至被稱爲一個類?
  3. 它是一個佔位符,直到我創建我自己的collectionViewSource類?這樣可行嗎?

我已經試過到目前爲止是應該保存從註冊等表的用戶表的信息下面的方法:

private object SaveCurrent_CommandExecute(object param) 
{ 
    myAppEntities context = new myAppEntities(); 

    myApp.signup Signup = new signup(); 

    try 
    { 
     myApp.user User = new Medcare2.user 
     { 
      firstname = Signup.first, 
      surname = Signup.last, 
      username = Signup.username, 
      password = Signup.password, 
     }; 

     // Persist Changes to database 
     context.users.Add(User); 
     context.SaveChanges(); 
     MessageBox.Show("Saved"); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

上述方法的問題在於,它創造一個我正在保存的註冊類的新實例,所以我抓取的屬性中的所有信息都是空的。所以我得到驗證錯誤。基本上。任何幫助將非常感激。

回答

1

無論何種控件調用SaveCurrent命令,您都需要將所選的Signup對象作爲CommandParameter傳遞。然後,您可以將object param轉換回Signup並從中提取值。

+0

正確,如果你想傳遞完整的對象,然後使用'CommandParameter =「{綁定路徑=。}」 – XAMlMAX 2014-10-28 15:17:29

相關問題