【问题标题】:Selected Item From ViewModel to Update using EF使用 EF 更新从 ViewModel 中选择的项目
【发布时间】:2012-12-05 12:42:40
【问题描述】:

我正在使用 WPF 创建一个应用程序,以使组织能够将不同的数据输入到应用程序中。我有一个选项卡控件来允许他们执行此操作。

然后在一个单独的视图中,我有一系列不同的数据网格,向用户显示他们已将哪些数据插入到数据库中。包含用于添加、更新或删除所需数据的按钮。

这引出了我的问题。目前,我可以轻松、毫无问题地删除和添加数据。但随后我的问题是尝试更新selected 项目,但它没有更新,导致null reference exception

如果我以编程方式设置我的属性属性,它会很好地更新它。像这样;public int _OrganisationTypeDetailID = 17;public int _OrganisationTypeID = 1;但我不想要这个,因为我希望用户能够自己选择并更新他们需要的数据。

这里有一些代码可能有助于解决我的问题;

查看模型;

    public void UpdateOrganisationTypeDetail(OrganisationTypeDetail orgTypeDetail)
    {
        using (DBEntities context = new DBEntities())
        {
            var orgTD = context.OrganisationTypeDetails.Where(otd => otd.OrganisationTypeDetailID == SelectedType.OrganisationTypeDetailID).FirstOrDefault();

            if (orgTD != null)
            {
                orgTD.Title = Title;
                orgTD.FirstName = FirstName;
                orgTD.Surname = Surname;
                orgTD.Position = Position;
                orgTD.DateOfBirth = DateOfBirth;
                orgTD.Address = Address;
                orgTD.Country = Country;
                orgTD.Postcode = Postcode;
                orgTD.PhoneNumber = PhoneNumber;
                orgTD.MobileNumber = MobileNumber;
                orgTD.FaxNumber = FaxNumber;
                orgTD.Email = Email;
                orgTD.NINumber = NINumber;

                //context.OrganisationTypeDetails.Attach(orgTD);
                context.OrganisationTypeDetails.ApplyCurrentValues(orgTD);
                context.SaveChanges();

                MessageBox.Show("Updated Organisation Type Details");
            }
            else
            {
                MessageBox.Show("Unable to update selected 'Type'.");
            }
        }

    private OrganisationTypeDetail _SelectedType;
    public OrganisationTypeDetail SelectedType
    {
        get
        {
            return _SelectedType;
        }
        set
        {
            if (_SelectedType == value)
                return;

            _SelectedType = value;
            OnPropertyChanged("SelectedType");
        }
    }

    public List<OrganisationTypeDetail> GetOrganisationTypeDetail //Loads data 
    {
        get
        {
            using (DBEntities context = new DBEntities())
            {
                var query = from e in context.OrganisationTypeDetails
                            select e;
                return query.ToList<OrganisationTypeDetail>();
            }
        }
    }

    private ICommand showUpdateCommand;
    public ICommand ShowUpdateCommand //Update command
    {
        get
        {
            if (showUpdateCommand == null)
            {
                showUpdateCommand = new RelayCommand(this.UpdateFormExecute, this.UpdateFormCanExecute); //i => this.UpdateOrganisationTypeDetail()
            }
            return showUpdateCommand;
        }
    }

代码隐藏;

    private void btnUpdateOrgTypeDetail_Click(object sender, RoutedEventArgs e)
    {
        OrganisationTypeDetail selected = dgOrgTypeDetail.SelectedItem as OrganisationTypeDetail;
        OrganisationTypeDetailViewModel org = new OrganisationTypeDetailViewModel();

        if (selected == null)
            MessageBox.Show("You must select a 'Type' before updating.");
        else
        {
            OrganisationTypeDetailUpdateView update = new OrganisationTypeDetailUpdateView();

            update.ShowDialog();
            org.UpdateOrganisationTypeDetail(selected);
            Page_Loaded(null, null);
        }
    }

xaml;

            <DataGrid Name="dgOrgTypeDetail" Height="145" Width="555" 
            IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding GetOrganisationTypeDetail}"
            SelectedItem="{Binding SelectedType, Mode=TwoWay}">

希望这个问题可以解决。

【问题讨论】:

    标签: c# wpf entity-framework datagrid


    【解决方案1】:

    我会说你最好的选择是使用 MVVM 模式中的命令来实现这一点。 看起来您正在使用 MVVM 和代码的组合,并在单击事件触发时实际创建视图模型的新实例。尝试在视图后面的代码中将视图模型绑定到您的视图作为数据上下文,然后尝试更新所选类型..

    此外,当您尝试对 SelectedType 进行更新时 - 使用 Snoop 查看您的视图 - 查看 SelectedType 属性是否仍绑定到视图。

    ICommand UpdateOrgTypeDetail { get;} 
    

    然后在视图模型构造函数中声明新实例

    UpdateOrgTypeDetail = new DelegateCommand<object>(ExecuteUpdateOrgTypeDetail,    CanExecuteUpdateOrgTypeDetail);
    

    然后,这两个代表将允许您单击按钮(需要绑定到 UpdateOrgTypeDetail)

    <Button Command="{Binding UpdateOrgTypeDetail}" />
    

    您应该会发现属性的更新是从这里正确完成的。

    【讨论】:

    • 感谢您的回复。好吧,如果我只使用 MVVM - 如何创建一个新窗口的弹出窗口供用户输入进行更新?
    • 最好的方法是使用 IDialogService 接口。这是一篇关于代码项目的好文章,描述了如何做到这一点codeproject.com/Articles/36745/…
    • 如果你对我的回答感到满意 - 给它一个漂亮的绿色勾号!
    猜你喜欢
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2014-07-17
    • 2021-10-26
    相关资源
    最近更新 更多