如何取消通过数据绑定所做的更改? [英] How to cancel changes made through Databinding?

查看:110
本文介绍了如何取消通过数据绑定所做的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过构造函数传递客户的名单。那么它的数据绑定到一个列表框。我还一个数据绑定文本框,允许改变客户的名称,它会自动更新列表框和客户名单,这是非常好的。



不过,我想就像不能维持的变化,如果用户点击取消按钮。我说的是程序的客户列表设置为旧的,但它不工作,当我打开窗口中再次ListBox中显示更新的客户名称而不是旧的名称。

 使用系统; 
使用System.Drawing中;使用System.Windows.Forms的
;
使用System.Collections.Generic;

命名空间Bingding_Test
{
公共部分类Form_Customers:表
{
名单,LT;客户>顾客;
名单,LT;客户> old_customers;
的BindingSource BS =新的BindingSource();

公共Form_Actors(列表<客户> _customers)
{
的InitializeComponent();

客户= _customers;
old_customers =新的List<客户>(_客户);

bs.DataSource =客户;
listBox1.DataSource = BS;
listBox1.DisplayMember =姓名;

txtb_name.DataBindings.Add(文本,BS,姓名);
}

无效Btn_cancelClick(对象发件人,EventArgs五)
{
=演员old_customers;
this.Close();
}

无效Btn_saveClick(对象发件人,EventArgs五)
{
this.Close();
}
}
}



任何人都知道我能做些什么以确保所有的更改将不保存,当我点击取消按钮?


解决方案

据我的理解,你的问题大约是使人们有可能取消你的列表与下所做的更改;通过 txtb_name ;客户过夜。默认情况下,数据绑定自动应用变化,当验证通过。您可以控制 DataSourceUpdateMode 来改变这种行为,以不同的方式,这取决于您的实现:




  1. 在您设置的 txtb_name (应尽可能与您的代码是怎么写的)任何绑定:

      txtb_name.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.Never; 


  2. 在您设置 txtb_name (也有可能与您的代码):

     的foreach(在txtb_name.DataBindings绑定b)
    {
    b.DataSourceUpdateMode = DataSourceUpdateMode.Never;
    }


  3. 在您设置使用的 BindingManagerBase (的的CurrencyManager 或的 PropertyManager中) - 比较通用的做法,因为它可以让你处理一次性许多控件,当你的项目的发展可能会非常有用。



     的foreach 
    {
    b.DataSourceUpdateMode = DataSourceUpdateMode.Never(在bindingManager.Bindings绑定b);
    }




有关的复杂的设置可以使用的 ResumeBinding 和的 SuspendBinding的 =http://msdn.microsoft.com/en-us/库/ system.windows.forms.bindingmanagerbase.aspx相对=nofollow> BindingManagerBase ,它们可以重写以提供验证定制的行为,例如,如果多个领域需要验证作为一个整体逻辑片,而不是一个接一个。



请注意,即使你设置 DataSourceUpdateMode = DataSourceUpdateMode.Never ,时时会被一个更新过,根据MSDN 到来(见备注部分):




即使在更新模式设置为从不,数据源将是
更新时,结合首先创建的,所以在该至少一个更新
总是会发生。




如果这是不适合你上可接受的,也就是说你正在建设有非常严格的要求的企业级应用,我建议手动处理一切 - 停止使用数据绑定



<在 DataSourceUpdateMode 信贷的提示>去弗雷德里克Gheysels他有关的的WinForms:在撤销对话框表数据绑定


I'm passing a list of customers via the constructor. Then it's databound to a ListBox. I've also databound a Textbox to allow changing the name of the customer, it automatically update the ListBox and the customer list, which is really nice.

However, I would like the changes not to be maintained if the user click on the Cancel Button. I'm telling the program to set the list of customers to the old one, but it doesn't work, when I open the window again the ListBox show the updated customer names rather than being the old names.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

namespace Bingding_Test
{
    public partial class Form_Customers : Form
    {
        List<Customer> customers;
        List<Customer> old_customers;
        BindingSource bs = new BindingSource();

        public Form_Actors(List<Customer> _customers)
        {
            InitializeComponent();

            customers = _customers;
            old_customers = new List<Customer>(_customers);

            bs.DataSource = customers;
            listBox1.DataSource = bs;
            listBox1.DisplayMember = "Name";

            txtb_name.DataBindings.Add("Text", bs, "Name");
        }

        void Btn_cancelClick(object sender, EventArgs e)
        {
            actors = old_customers;
            this.Close();
        }

        void Btn_saveClick(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Anyone know what I can do to make sure all the changes aren't saved when I click the cancel button?

解决方案

As far as I understood, your question is about making possible to cancel changes made to your List<Customer> through txtb_name. By default, DataBindings automatically applies changes, when validation passed. You can control DataSourceUpdateMode to change this behavior, in different ways, depending on your implementation:

  1. Before you set up any bindings on txtb_name (should be possible with how your code is written):

    txtb_name.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.Never;
    

  2. After you set bindings on txtb_name (also possible with your code):

    foreach( Binding b in txtb_name.DataBindings)
    {
        b.DataSourceUpdateMode = DataSourceUpdateMode.Never;
    }
    

  3. After you set bindings using BindingManagerBase (CurrencyManager or PropertyManager) - more generic approach, because it allows you to handle many controls in one shot, may get useful when your project evolves.

    foreach(Binding b in bindingManager.Bindings)
    {
        b.DataSourceUpdateMode = DataSourceUpdateMode.Never;
    }
    

For complex setups you can use ResumeBinding and SuspendBinding of BindingManagerBase, they can be overriden to provide custom behavior on validation, for example, if multiple fields needs to validated as a whole logical piece, rather than one by one.

Please note that even if you set DataSourceUpdateMode = DataSourceUpdateMode.Never, there will always be one update coming through, according to MSDN (see Remarks section):

Even when the update mode is set to Never, the data source will be updated when the binding is first created, so at that least one update will always occur.

If this is not acceptable for you, i.e. you are building an enterprise-class application with very strict requirements, I suggest handling everything manually - stop using DataBindings.

Credit for the hint at DataSourceUpdateMode goes to Frederik Gheysels for his article about WinForms: DataBinding on a cancellable Dialog Form.

这篇关于如何取消通过数据绑定所做的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆