简体   繁体   English

数值更改时,NumericUpDown DataBinding属性不会更新

[英]NumericUpDown DataBinding property not updating when value changes

I am attempting to perform a DataBinding on a NumericUpDown WinForm control. 我正在尝试对NumericUpDown WinForm控件执行DataBinding。 Performing the binding works as designed, but I am having an issue with the value not being pushed to the binded property until the element goes out of focus. 执行绑定按设计工作,但是我遇到一个问题,直到元素失去焦点,该值才被推送到绑定属性。 Is there something I am missing to get the property to update when the value changes in the control without requiring the focus to be lost? 当控件中的值发生更改而又不需要丢失焦点时,是否缺少一些东西来使属性更新?

If this is working as designed, is there a way to force the property update without losing focus? 如果这按设计工作,是否有办法在不失去焦点的情况下强制更新属性?

Logic: 逻辑:

using System;
using System.Windows.Forms;

public partial class Form1 : Form
{
    private NumericUpDown numericUpDown1 = new NumericUpDown();
    private ExampleData _ed = new ExampleData();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Define the UI Control
        numericUpDown1.DecimalPlaces = 7;
        numericUpDown1.Location = new System.Drawing.Point(31, 33);
        numericUpDown1.Name = "numericUpDown1";
        numericUpDown1.Size = new System.Drawing.Size(120, 20);
        numericUpDown1.TabIndex = 0;

        // Add the UI Control
        Controls.Add(numericUpDown1);

        // Bind the property to the UI Control
        numericUpDown1.DataBindings.Add("Value", _ed, nameof(_ed.SampleDecimal));

        numericUpDown1.ValueChanged += NumericUpDown1_ValueChanged;
    }

    private void NumericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        // This will fire as you change the control without losing focus.
        System.Diagnostics.Debugger.Break();
    }
}

public class ExampleData
{
    public decimal SampleDecimal
    {
        get { return _sampleDecimal; }
        set
        {
            // This set isn't called until after you lose focus of the control.
            System.Diagnostics.Debugger.Break();
            _sampleDecimal = value;
        }
    }

    private decimal _sampleDecimal = 1.0m;
}

Change your binding to this: 更改对此的绑定:

numericUpDown1.DataBindings.Add(nameof(NumericUpDown.Value), _ed, nameof(ExampleData.SampleDecimal), false, DataSourceUpdateMode.OnPropertyChanged);

This will ensure that the binding fires when the value changes rather than when you move focus away from the control. 这将确保在值更改时(而不是在将焦点从控件移开时)触发绑定。

If you then want to be able to update the SampleDecimal from code and have it update on your numericupdown you'd need to implement the INotifyPropertyChanged interface on your SampleData class, like this: 然后,如果您希望能够从代码中更新SampleDecimal并使其在您的numericupdown中更新,则需要在SampleData类上实现INotifyPropertyChanged接口,如下所示:

public class ExampleData : INotifyPropertyChanged
{

    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public decimal SampleDecimal
    {
        get { return _sampleDecimal; }
        set
        {
            _sampleDecimal = value;
            OnPropertyChanged();
        }
    }

    private decimal _sampleDecimal = 1.0m;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM