为什么在控件值更改后不更新绑定项

本文关键字:更新 绑定 控件 为什么 | 更新日期: 2023-09-27 17:54:39

我很难绑定属性。我可能遗漏了什么。问题是,直到我与UI进行另一次交互,比如按下一个空的虚拟按钮,我的对象属性才会被界面控件更改更新。这真的很奇怪。我错过了什么?如何使绑定属性在控件检查更改时更新?

示例:

    创建一个空表单,添加一个复选框和一个按钮。
  • 将复选框绑定到IsOnSale Car属性
  • 添加一个控制台写入行来查看何时IsOnSale属性将被更改
  • 构建并点击复选框,IsOnSale不会改变(没有控制台消息),直到点击另一个按钮或其他东西。
  • 点击dummy按钮,IsOnSale属性将被更改!什么! ? ?

    private Car car;
    public Form1()
    {
        InitializeComponent();
        car = new Car();
        car.IsOnSale = false;
        checkBox1.DataBindings.Add("Checked", car, "IsOnSale");
    }
    //U dont need this for the test..
    private void btnPrintStatus_Click(object sender, EventArgs e)
    {
        string s = car.ToString();
        Console.WriteLine(s);
    }
    

Car只是一个实现INotifyPropertyChanged的类。此外,每次发出属性更改请求时,它都会在控制台上打印(只是为了调试)。正如您所看到的,如果您构建了示例,那么更改请求只会在单击虚拟按钮之后发出…

internal class Car : INotifyPropertyChanged, INotifyPropertyChanging
{
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            string propName = GetName(new { Name });
            UpdateField(ref name, value, propName, null);
        }
    }
    private bool isOnSale;
    public bool IsOnSale
    {
        get { return isOnSale; }
        set
        {
            string propName = GetName(new { IsOnSale });
            UpdateField(ref isOnSale, value, propName, null);
        }
    }
    public override string ToString()
    {
        return string.Format("Car Name: {0}. IsOnSale: {1}", Name, IsOnSale);
    }
    #region  INotifyPropertyChanged, INotifyPropertyChanging
    public event PropertyChangingEventHandler PropertyChanging;
    public event PropertyChangedEventHandler PropertyChanged;
    public static string GetName<T>(T item) where T : class
    {
        return typeof(T).GetProperties()[0].Name;
    }
    protected bool UpdateField<T>(ref T field, T newValue, string propertyName, Action action = null)
    {
        bool needChange = !EqualityComparer<T>.Default.Equals(field, newValue);
        Console.WriteLine("Try to UpdateField {0}. Need update? {1}", propertyName, needChange);
        if (needChange)
        {
            OnPropertyChanging(propertyName);
            field = newValue;
            if (action != null)
                action();
            OnPropertyChanged(propertyName);
        }
        return needChange;
    }
    protected void OnPropertyChanging(string propertyName)
    {
        PropertyChangingEventHandler handler = PropertyChanging;
        if (handler != null)
            handler(this, new PropertyChangingEventArgs(propertyName));
    }
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion
}

编辑-变通

我已经能够找到一个工作,但它闻起来…我使用BackgroundWorker来"退出"更改事件,之后我执行单击虚拟按钮。

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        //Wont work, need to get out of this event.
        //btnDummy.PerformClick();
        //Using another thread to get out of this event:
        RunAsyncBindFixer();
    }
    private void RunAsyncBindFixer()
    {
        var workerBindFixer = new BackgroundWorker();
        workerBindFixer.DoWork += WorkerBindFixer_DoWork;
        workerBindFixer.RunWorkerCompleted += WorkerBindFixer_RunWorkerCompleted;
        Console.WriteLine("Starting RunAsyncBindFixer");
        workerBindFixer.RunWorkerAsync();
    }
    void WorkerBindFixer_DoWork(object sender, DoWorkEventArgs e)
    {
        //Aparently we need nothing here.
        //Thread.Sleep(0);
    }
    void WorkerBindFixer_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Console.WriteLine("RunAsyncBindFixer RunWorkerCompleted");
        btnDummy.PerformClick();
        //Checked change on a dummy checkbox wont work also
        //ckbDummy.Checked = !ckbDummy.Checked;
        Console.WriteLine("Dummy action applied");
    }

为什么在控件值更改后不更新绑定项

Databinding默认在绑定控件验证时写入值,这通常发生在控件失去焦点时(例如在您的示例中按下按钮时)

你可以强制数据绑定使用控件的onpropertychanged,只要控件支持它(在本例中是CheckedChanged,它由复选框控件支持)

checkBox1.DataBindings.Add("Checked", car, "IsOnSale", true, DataSourceUpdateMode.OnPropertyChanged);