简体   繁体   English

为什么“绑定文本框”不修改其关联属性,因为“文本”值是通过编程方式设置的?

[英]Why does a Bound Textbox not modify its associated property is the Text value is set programmatically?

I have a Windows Form with a number of Textboxes. 我有一个带有多个文本框的Windows窗体。 I bind them to a DataSource as the Form Loads. 我在表单加载时将它们绑定到数据源。

public partial class FormSettings : Form
{
    private readonly DbContext _context = new DbContext();

    public FormSettings()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        _context.OrderEntrySettings.Load();

        SettingsBindingSource.DataSource = _context.OrderEntrySettings.Local.ToBindingList();

        Binding saNbinding = new Binding("Text", SettingsBindingSource, "InvoiceNumber");

        InvoiceNumberTextBox.DataBindings.Add(saNbinding);

        Binding pthNbinding = new Binding("Text", SettingsBindingSource, "SalesOrderExportPath");

        PathTextBox.DataBindings.Add(pthNbinding);

    <snip>
    …   
    </snip>
}

One of the Textboxes is bound to a Directory Path (string) Property. 文本框之一绑定到目录路径(字符串)属性。 If I type in a new directory in the Path Textbox and hit my save button, The path saves just fine. 如果我在“路径”文本框中输入新目录,然后单击“保存”按钮,则路径会保存得很好。

But, If I change the Text Property on the Textbox to the SelectedPath from a FolderBrowserDialog dialog and then hit save, the Textbox Text shows the directory I selected but the Entity is not modified and nothing gets saved back to the database. 但是,如果我从FolderBrowserDialog对话框将Textbox上的Text属性更改为SelectedPath,然后单击Save,则Textbox Text将显示我选择的目录,但是Entity不会被修改,也不会保存任何内容回到数据库。

As a workaround, I set both the Path Textbox Text and set the Property from the context to the SelectedPath. 作为一种解决方法,我同时设置了路径文本框文本并将属性从上下文设置为SelectedPath。

Why does a Bound Textbox not modify its associated property if the Text value is set programmatically? 如果“文本”值是通过编程设置的,为什么“绑定文本框”不修改其关联属性?

Here is the reason. 这是原因。 Binding class has a property called DataSourceUpdateMode with 3 options - Never , OnPropertyChanged and OnValidation , the last being a default. 结合类有一个叫做财产DataSourceUpdateMode带有3个选项- NeverOnPropertyChangedOnValidation最后被默认。 When you set the Text property programatically, there is no validating/validated events because they fire only when the control is on focus and has been edited, so the binding does not update the data source. 以编程方式设置Text属性时,没有验证/已验证事件,因为它们仅在控件处于焦点且已被编辑时才触发,因此绑定不会更新数据源。

That being said, here are the options you have: 话虽这么说,以下是您可以选择的选项:

(A) Do not set the control property programatically, set the data source property instead. (A)不要以编程方式设置控件属性,而是设置数据源属性。

(B) Change the binding DataSourceUpdateMode to OnPropertyChanged . (B)将绑定的DataSourceUpdateMode更改为OnPropertyChanged This however will cause the data source property to be updated on every char that the user is typing. 但是,这将导致在用户键入的每个字符上更新数据源属性。

(C) Use the following snippet: (C)使用以下代码段:

yourTextBox.Text = ...;
yourTextBox.DataBindings["Text"].WriteValue();

If memory serves, data binding in WinForms (and possibly WebForms) uses the changes in control focus to detect when the data in the field has changed. 如果有内存,则WinForms(可能还有WebForms)中的数据绑定将使用控件焦点中的更改来检测字段中的数据何时更改。

When you are modifying the values of the controls in the code-behind, you generally aren't changing the control focus. 在后面的代码中修改控件的值时,通常不会更改控件焦点。 Consequently, there isn't anything that pokes the data-binding engine in the ribs and clues it in to the fact that it needs to re-evaluate the data. 因此,没有任何东西可以刺破数据绑定引擎,并暗示它需要重新评估数据。

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

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