【问题标题】:save row in one table to another table using linq使用 linq 将一个表中的行保存到另一个表
【发布时间】:2014-12-13 16:01:08
【问题描述】:

您知道何时将表格从数据源窗口拖放到用户控件上吗? Visual Studio 会自动创建一个 datacontext(在视图的 xaml 中)以将页面上的新元素绑定到。这个 dataContext 设置为一个 collectionViewSource,这个 collectionViewSource 从数据集中绘制它的信息。拖放时获取表格的数据集相同。

在我的场景中,这是它创建的:

<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 类中,这是我的问题开始的地方。

我需要将“注册”表中的相同信息保存到用户表中。

  1. 既然文本框绑定的属性在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);
    }
}  

上述方法的问题在于它创建了我从中保存的注册类的新实例,因此我正在获取的属性中的所有信息都是空的。所以我得到验证错误。基本上。任何帮助将不胜感激。

【问题讨论】:

    标签: c# wpf xaml mvvm


    【解决方案1】:

    您需要在调用SaveCurrent 命令的任何控件上将您选择的Signup 对象作为CommandParameter 传递。然后您可以将object param 转换回Signup 并从中提取值。

    【讨论】:

    • 正确,如果要传递完整对象,请使用 'CommandParameter="{Binding Path=.}"
    猜你喜欢
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多